本文共 6091 字,大约阅读时间需要 20 分钟。
org.springframework.boot spring-boot-starter-security
import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.builders.WebSecurity;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration@EnableWebSecuritypublic class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/","/login", "/ws").permitAll() //根路径和/login、/ws不拦截 .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") //登陆页面 .defaultSuccessUrl("/chat") //登录成功转向该页面 .permitAll() .and() .logout() .permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("pyc").password("pyc").roles("USER") .and() .withUser("ycy").password("ycy").roles("USER"); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("src/main/resources/static/**"); //静态资源放行 }}
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@EnableWebSocketMessageBrokerpublic class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/endpointWisely").withSockJS(); //注册协议节点,指定用 Sock JS registry.addEndpoint("/endpointChat").withSockJS(); //增加协议节点 } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.enableSimpleBroker("/queue", "/topic"); // 消息代理 }}
登陆页面 无效的账号和密码你已注销
Home 聊天室
import com.wisely.ch5_2_3.domain.WiselyMessage;import com.wisely.ch5_2_3.domain.WiselyResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.messaging.handler.annotation.MessageMapping;import org.springframework.messaging.handler.annotation.SendTo;import org.springframework.stereotype.Controller;import org.springframework.messaging.simp.SimpMessagingTemplate;import java.security.Principal;@Controllerpublic class WsController { @MessageMapping("/welcome") @SendTo("/topic/getResponse") public WiselyResponse say(@org.jetbrains.annotations.NotNull WiselyMessage message) throws Exception{ Thread.sleep(300); return new WiselyResponse("Welcome,"+message.getName()+"!"); } // 以下为点对点式套接字的 Controller @Autowired private SimpMessagingTemplate messagingTemplate; @MessageMapping("/chat") public void handleChat(Principal principal,String msg){ if(principal.getName().equals("pyc")){ messagingTemplate.convertAndSendToUser("ycy", "/queue/notifications",principal.getName()+"-send:"+msg); }else{ messagingTemplate.convertAndSendToUser("pyc", "/queue/notifications",principal.getName()+"-send:"+msg); } }}
import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configurationpublic class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry){ registry.addViewController("/ws").setViewName("/ws"); registry.addViewController("/login").setViewName("/login"); registry.addViewController("/chat").setViewName("/chat"); }}
转载地址:http://hoqgn.baihongyu.com/