| @Component("customAuthenticationSuccessHandler") |
| public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler { |
| @Override |
| public void onAuthenticationSuccess(HttpServletRequest request, |
| HttpServletResponse response, Authentication authentication) throws IOException, ServletException { |
| MengxueguResult result = MengxueguResult.ok("认证成功"); |
| response.setContentType("application/json;charset=UTF-8"); |
| response.getWriter().write(result.toJsonString()); |
| } |
| } |
| |
| @Component("customAuthenticationFailureHandler") |
| public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler { |
| @Override |
| public void onAuthenticationFailure(HttpServletRequest request, |
| HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { |
| MengxueguResult result = MengxueguResult.build(HttpStatus.UNAUTHORIZED.value(), exception.getMessage()); |
| response.setContentType("application/json;charset=UTF-8"); |
| response.getWriter().write(result.toJsonString()); |
| } |
| } |
| @Component("customAuthenticationSuccessHandler") |
| public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler { |
| Logger logger = LoggerFactory.getLogger(getClass()); |
| @Autowired |
| SecurityProperties securityProperties; |
| @Override |
| public void onAuthenticationSuccess(HttpServletRequest request, |
| HttpServletResponse response, Authentication authentication) throws IOException, ServletException { |
| if(LoginResponseType.JSON.equals( |
| securityProperties.getAuthentication().getLoginType())) { |
| |
| MengxueguResult result = MengxueguResult.ok("认证成功"); |
| response.setContentType("application/json;charset=UTF-8"); |
| response.getWriter().write(result.toJsonString()); |
| }else { |
| |
| logger.info("authentication: " + JSON.toJSONString(authentication)); |
| super.onAuthenticationSuccess(request, response, authentication); |
| } |
| } |
| } |
| |
| @Component("customAuthenticationFailureHandler") |
| public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler { |
| @Autowired |
| SecurityProperties securityProperties; |
| @Override |
| public void onAuthenticationFailure(HttpServletRequest request, |
| HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { |
| if(LoginResponseType.JSON.equals(securityProperties.getAuthentication().getLoginType())) { |
| |
| MengxueguResult result = MengxueguResult.build(HttpStatus.UNAUTHORIZED.value(), exception.getMessage()); |
| response.setContentType("application/json;charset=UTF-8"); |
| response.getWriter().write(result.toJsonString()); |
| }else { |
| |
| super.setDefaultFailureUrl(securityProperties.getAuthentication().getLoginPage()+"?error"); |
| super.onAuthenticationFailure(request, response, exception); |
| } |
| } |
| } |
| @Component("customAuthenticationFailureHandler") |
| |
| public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler { |
| @Autowired |
| SecurityProperties securityProperties; |
| @Override |
| public void onAuthenticationFailure(HttpServletRequest request, |
| HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { |
| if(LoginResponseType.JSON.equals(securityProperties.getAuthentication().getLoginType())) { |
| |
| MengxueguResult result = MengxueguResult.build(HttpStatus.UNAUTHORIZED.value(), exception.getMessage()); |
| response.setContentType("application/json;charset=UTF-8"); |
| response.getWriter().write(result.toJsonString()); |
| }else { |
| |
| |
| |
| String referer = request.getHeader("Referer"); |
| logger.info("referer:" + referer); |
| String lastUrl = StringUtils.substringBefore(referer,"?"); |
| logger.info("上一次请求的路径 :" + lastUrl); |
| super.setDefaultFailureUrl(lastUrl+"?error"); |
| super.onAuthenticationFailure(request, response, exception); |
| } |
| } |
| } |
| |
| smsCodeAuthenticationFilter.setRememberMeServices( http.getSharedObject(RememberMeServices.class)); |
| |
| |
| <input name="remember-me" type="checkbox" id="remember">记住我 |
| |
| |
| return new User("meng", "", true, true, true, true, AuthorityUtils.commaSeparatedStringToAuthorityList("ADMIN")); |
| import org.springframework.security.core.Authentication; |
| import org.springframework.security.core.annotation.AuthenticationPrincipal; |
| import org.springframework.security.core.context.SecurityContextHolder; |
| import org.springframework.security.core.userdetails.UserDetails; |
| import org.springframework.stereotype.Controller; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| import org.springframework.web.bind.annotation.ResponseBody; |
| import java.util.Map; |
| |
| @Controller |
| public class MainController { |
| @RequestMapping({"/index", "/", ""}) |
| public String index(Map<String, Object> map) { |
| Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| if(principal != null && principal instanceof UserDetails) { |
| UserDetails userDetails = (UserDetails)principal; |
| String username = userDetails.getUsername(); |
| map.put("username", username); |
| } |
| return "index"; |
| } |
| |
| @RequestMapping("/user/info") |
| @ResponseBody |
| public Object userInfo(Authentication authentication) { |
| return authentication.getPrincipal(); |
| } |
| |
| @RequestMapping("/user/info2") |
| @ResponseBody |
| public Object userInfo2(@AuthenticationPrincipal UserDetails userDetails) { |
| return userDetails; |
| } |
| |
| } |
| |
| # yml中配置 |
| mengxuegu: |
| security: |
| authentication: |
| imageCodeUrl: /code/image # 获取图形验证码地址 |
| mobileCodeUrl: /code/mobile # 发送手机验证码地址 |
| mobilePage: /mobile/page # 前往手机登录页面 |
| tokenValiditySeconds: 604800 # 记住我功能有效时长 |
| |
| # 默认配置 |
| @Data |
| public class AuthenticationProperties { |
| private String loginPage = "/login/page"; |
| private String loginProcessingUrl = "/login/form"; |
| private String usernameParameter = "name"; |
| private String passwordParameter = "pwd"; |
| private String[] staticPaths = {"/dist/**", "/modules/**", "/plugins/**"}; |
| private LoginResponseType loginType = LoginResponseType.REDIRECT; |
| private String imageCodeUrl = "/code/image"; |
| private String mobileCodeUrl = "/code/mobile"; |
| private String mobilePage = "/mobile/page"; |
| private Integer tokenValiditySeconds = 60*60*24*7; |
| } |
| |
| # 在SpringSecurityConfig配置类中使用 |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结