✨ 新增webSocket

This commit is contained in:
396264893 committed 2019-01-16 12:01:27 +08:00
1 parent 074fe4af7d
commit b002c09add
4 files changed
+39 -2

No files matched your search

+5
View File
@@ -72,6 +72,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--websocket-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<!--spring security 、oauth、jwt依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
@@ -69,7 +69,7 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
.antMatchers(
"/token/**",
"/actuator/**",
"/mobile/**").permitAll()
"/mobile/**","/ws/**").permitAll()
.anyRequest().authenticated()
.and().csrf().disable()
.apply(mobileSecurityConfigurer());
@@ -0,0 +1,23 @@
package com.pig4cloud.pigx.auth.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws")
.setAllowedOrigins("*")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
//registry.enableSimpleBroker("/loginToAll");
registry.setUserDestinationPrefix("/user/");
}
}
@@ -18,7 +18,10 @@
package com.pig4cloud.pigx.auth.handler;
import com.pig4cloud.pigx.common.security.handler.AbstractAuthenticationSuccessEventHandler;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
@@ -30,6 +33,9 @@ import org.springframework.stereotype.Component;
@Component
public class PigxAuthenticationSuccessEventHandler extends AbstractAuthenticationSuccessEventHandler {
@Autowired
SimpMessagingTemplate simpMessagingTemplate;
/**
* 处理登录成功方法
* <p>
@@ -39,6 +45,9 @@ public class PigxAuthenticationSuccessEventHandler extends AbstractAuthenticatio
*/
@Override
public void handle(Authentication authentication) {
log.info("用户:{} 登录成功", authentication.getPrincipal());
Object principal = authentication.getPrincipal();
log.info("用户:{} 登录成功", principal);
PigxUser pigxUser = (PigxUser) principal;
simpMessagingTemplate.convertAndSendToUser(pigxUser.getTenantId().toString(),"/loginToAll",pigxUser.getUsername());
}
}