我正在尝试在websocket + spring security + springBoot上编写自己的聊天记录。现在我在我的项目中:
Security-context.xml:
...
<intercept-url pattern="/login/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/resources/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login/>
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="bob" password="test" authorities="ROLE_USER"/>
<user name="jim" password="test" authorities="ROLE_USER"/>
<user name="a" password="1" authorities="ROLE_USER"/>
...
servlet-context.xml
<context:annotation-config/>
<context:component-scan base-package="wsproblem" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/ws">
<websocket:sockjs />
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic, /user" />
</websocket:message-broker>
,我有标准 web.xml 和 root-context.xml 。
我还有一个简单的控制器:
@Controller
@RequestMapping("/")
public class HomeController {
@Autowired private SimpMessagingTemplate simpMessagingTemplate;
@RequestMapping(method = RequestMethod.GET)
public String home() {
return "home";
}
@MessageMapping("/greeting")
public void greeting(Principal principal) {
String reply = "hello " + principal.getName();
System.out.println("sending " + reply);
simpMessagingTemplate.convertAndSendToUser(principal.getName(), "/reply", reply);
}
我的问题是:
如何将此代码(例如,如何迁移)从xml重写为带注释的Java类文件,例如 WebSecurityConfig扩展了AbstractSecurityWebSocketMessageBrokerConfigurer
答案 0 :(得分:0)
您可以很好地执行这样的操作
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
private static final Logger logger = LoggerFactory.getLogger(WebSocketConfig.class);
public WebSocketConfig(){
logger.info("---WebSocket---");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/temp-ws").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/temp-web");
registry.enableSimpleBroker("/queue"); // Enables a simple in-memory broker
// rabbitmq broker
// registry.enableStompBrokerRelay("/topic", "/queue")
// .setRelayHost("localhost")
// .setRelayPort(61613)
// .setClientLogin("guest")
// .setClientPasscode("guest");
}
}
答案 1 :(得分:0)
您可以在github上找到正在运行的项目。 https://github.com/tanoliusman/UrlShortener
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/gs-guide-websocket").withSockJS();
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> arg0) {
// TODO Auto-generated method stub
}
@Override
public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> arg0) {
// TODO Auto-generated method stub
}
@Override
public void configureClientInboundChannel(ChannelRegistration arg0) {
// TODO Auto-generated method stub
}
@Override
public void configureClientOutboundChannel(ChannelRegistration arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean configureMessageConverters(List<MessageConverter> arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public void configureWebSocketTransport(WebSocketTransportRegistration arg0) {
// TODO Auto-generated method stub
}
}
function connect() {
var socket = new SockJS('/gs-guide-websocket');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/greetings', function (greeting) {
showGreeting(greeting.body);
});
});
}
function disconnect() {
if (stompClient !== null) {
stompClient.disconnect();
}
setConnected(false);
console.log("Disconnected");
}
function sendName() {
stompClient.send("/app/hello", {}, JSON.stringify({'name': $("#name").val()}));
}
function showGreeting(message) {
var object = jQuery.parseJSON( message).name;
if(object == url){
value = 1;
}
}