- 在demo01的基础上继续开发
- web模块编写CustomUserDetailsService
| @Component("customUserDetailsService") |
| public class CustomUserDetailsService implements UserDetailsService { |
| |
| |
| Logger logger = LoggerFactory.getLogger(getClass()); |
| |
| @Autowired |
| PasswordEncoder passwordEncoder; |
| |
| @Override |
| public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { |
| logger.info("请求认证的用户名: " + username); |
| |
| if(!"admin".equalsIgnoreCase(username)) { |
| throw new UsernameNotFoundException("用户名或密码错误"); |
| } |
| |
| String password = passwordEncoder.encode("admin"); |
| |
| |
| |
| |
| return new User(username, password, |
| AuthorityUtils.commaSeparatedStringToAuthorityList("ADMIN")); |
| } |
| |
| } |
- core模块的SpringSecurityConfig类中注入
| @Autowired |
| UserDetailsService customUserDetailsService; |
| |
| @Override |
| protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| auth.userDetailsService(customUserDetailsService); |
| } |
- base模块新建工具类MengxueguResult
| @Data |
| public class MengxueguResult { |
| |
| |
| private Integer code; |
| |
| |
| private String message; |
| |
| |
| private Object data; |
| |
| public MengxueguResult() { |
| } |
| |
| public MengxueguResult(Object data) { |
| this.code = 200; |
| this.message = "OK"; |
| this.data = data; |
| } |
| |
| public MengxueguResult(String message, Object data) { |
| this.code = 200; |
| this.message = message; |
| this.data = data; |
| } |
| |
| public MengxueguResult(Integer code, String message, Object data) { |
| this.code = code; |
| this.message = message; |
| this.data = data; |
| } |
| |
| public static MengxueguResult ok() { |
| return new MengxueguResult(null); |
| } |
| |
| public static MengxueguResult ok(String message) { |
| return new MengxueguResult(message, null); |
| } |
| |
| public static MengxueguResult ok(Object data) { |
| return new MengxueguResult(data); |
| } |
| |
| public static MengxueguResult ok(String message, Object data) { |
| return new MengxueguResult(message, data); |
| } |
| |
| public static MengxueguResult build(Integer code, String message) { |
| return new MengxueguResult(code, message, null); |
| } |
| |
| public static MengxueguResult build(Integer code, String message, Object data) { |
| return new MengxueguResult(code, message, data); |
| } |
| |
| public String toJsonString() { |
| return JSON.toJSONString(this); |
| } |
| |
| |
| |
| |
| |
| |
| public static MengxueguResult format(String json) { |
| try { |
| return JSON.parseObject(json, MengxueguResult.class); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| return null; |
| } |
| |
| } |
- core模块新建CustomAuthenticationSuccessHandler、CustomAuthenticationFailureHandler
| @Component("customAuthenticationSuccessHandler") |
| public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler { |
| @Override |
| public void onAuthenticationSuccess(HttpServletRequest request, |
| HttpServletResponse response, Authentication authentication) throws IOException, ServletException { |
| System.out.println("CustomAuthenticationSuccessHandler ---> success"); |
| } |
| } |
| |
| @Component("customAuthenticationFailureHandler") |
| public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler { |
| @Override |
| public void onAuthenticationFailure(HttpServletRequest request, |
| HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { |
| System.out.println("CustomAuthenticationFailureHandler ---> error"); |
| } |
| } |
- 在SpringSecurityConfig类中注入并使用
| @Autowired |
| private AuthenticationSuccessHandler customAuthenticationSuccessHandler; |
| |
| @Autowired |
| private AuthenticationFailureHandler customAuthenticationFailureHandler; |
| |
| .successHandler(customAuthenticationSuccessHandler) |
| .failureHandler(customAuthenticationFailureHandler) |
| # 认证成功时,控制台打印 |
| 05:57:06.092 INFO 16552 |
| CustomAuthenticationSuccessHandler |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?