💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
1. 实现`AuthenticationSuccessHandler`接口 ```java @Component public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler { @Autowired private ObjectMapper mapper; @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { response.setContentType("application/json;charset=utf-8"); response.getWriter().write(mapper.writeValueAsString(authentication)); } } ``` 2. 修改`BrowserSecurityConfig`配置 ```java @Configuration public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler; @Override protected void configure(HttpSecurity http) throws Exception { http.formLogin() .loginPage("/login.html") .loginProcessingUrl("/login") .successHandler(myAuthenticationSuccessHandler) // 自定义登录成功处理逻辑 .and() .authorizeRequests() .antMatchers("/login.html").permitAll() .anyRequest() .authenticated() .and().csrf().disable(); } } ``` ###### 可以获取到登录成功后的认证信息 ```java SecurityContextHolder.getContext().getAuthentication() ``` 同理,自定义登录失败可以实现`onAuthenticationFailure`接口。此文就不再演示。