1
我有一個使用Spring MVC和Security.And的應用程序,我嘗試向它添加Websockets。我已經有成功的連接,但是,當我嘗試發送消息到後端 - 沒有任何事情發生。在使用@MessageMapping註釋的調試模式方法根本沒有達到!我不知道爲什麼。我已經嘗試了很多谷歌的解決方案,所以現在所有的配置都下一個:@MessageMapping不適用於Spring Security和MVC
的WebSockets的我也有增加的安全配置
@Configuration
public class SecuritySocketConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
protected boolean sameOriginDisabled() {
return true;
}
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
messages.simpDestMatchers("/hello1").authenticated().simpDestMatchers("/app/hello1").authenticated();//permitAll();
}
}
Controller類
@Controller
public class WebsocketController {
@MessageMapping("/hello1")
public void send(Message message) {
String name = message.getName();
}
}
插座.js,即導入JSP文件
;(function() {
//init
$(function() {
$("#control_mode").change(app.page.controlCheck);
connect();
});
var stompClient = null;
function connect() {
var socket = new SockJS("http://localhost:8080/hello1");
stompClient = Stomp.over(socket);
console.log('attempt to connect: ' +stompClient!=null);
console.log('session id: '+ "${pageContext.session.id}");
stompClient.connect('', '', function(frame) {
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/greetings/', function(result) {
getResult(result);
});
});
}
function getResult(result) {
var isControlable= JSON.parse(greeting.body).isControlable;
if (isControlable) {
alert('Control was already gained')
} else {
$("#control_mode").prop("checked", true);
}
}
app.page.controlCheck = function() {
stompClient.send('/app/hello1', {}, JSON.stringify({'name' : "alastor" }));
if (this.checked) {
$("#control_mode").prop("checked", !this.checked);
} else {
alert('was click for release control');
}
};
function disconnect() {
stompClient.disconnect();
console.log("Disconnected");
}
})();
彈簧security.xml文件(進一步被導入到主spring.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd">
<http pattern="/resources/**" security="none"/>
<http pattern="/webjars/**" security="none"/>
<http pattern="/rest/**" security="none"/>
<http pattern="/favicon.ico" security="none"/>
<http security="none" pattern="/pathWhereCSRFWillBeIgnored/**"/>
<http>
<intercept-url pattern="/welcome/**" access="permitAll"/>
<intercept-url pattern="/ajax/welcome/**" access="permitAll"/>
<intercept-url pattern="/ajax/**" access="permitAll"/> <!--todo for testing-->
<intercept-url pattern="/**" access="@validateService.isValid(request)"/>
<form-login login-page="/welcome"
authentication-failure-url="/welcome?error=true"
login-processing-url="/spring_security_check"
authentication-success-handler-ref="directLinkHandler"/>
<logout logout-success-url="/welcome"/>
<csrf disabled="true"/>
</http>
<beans:bean class="com.promptlink.stbtp.webapi.listener.AuthenticationEventListener"/>
<beans:bean class="com.promptlink.stbtp.service.security.util.PasswordEncoder" id="passwordEncoder"/>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userService">
<password-encoder ref="passwordEncoder"/>
</authentication-provider>
</authentication-manager>
</beans:beans>
瀏覽器日誌:
Opening Web Socket... stomp.js:134:99
Web Socket Opened... stomp.js:134:99
>>> CONNECT
login:
passcode:
accept-version:1.1,1.0
heart-beat:10000,10000
stomp.js:134:99
<<< CONNECTED
version:1.1
heart-beat:0,0
user-name:qwe
stomp.js:134:99
connected to server undefined stomp.js:134:99
Connected: CONNECTED
user-name:qwe
heart-beat:0,0
version:1.1
socket.js:18:13
>>> SUBSCRIBE
id:sub-0
destination:/topic/greetings/
stomp.js:134:99
>>> SEND
destination:/app/hello1
content-length:18
{"name":"alastor"}
做別人知道我在做什麼錯?順便說一句,當我在簡單的項目中使用這個配置時,沒有Spring-Security,一切都很完美!