和视屏上面这一张输如错误的验证码不一样,找了很久的都找不出来,请问这是为什么?
来源:4-7 图片验证码

Minhatl
2019-11-06
进入了登录失败后的处理器然后返回了这个401页面
写回答
1回答
-
Minhatl
提问者
2019-11-06
一句话想吐槽
已解决,原因是老师没有讲到这里需要改请比较下面代码:
@Component("imoocAuthenctiationFailureHandler") public class ImoocAuthenctiationFailureHandlerextends SimpleUrlAuthenticationFailureHandler { private static final Logger logger = LoggerFactory.getLogger(MinhatAuthenctiationFailureHandler.class); @Autowired private SecurityProperties securityProperties; /** * 吧对象转为json字符串,SpringMvc在启动的时候回为我们注册一个 */ @Autowired private ObjectMapper objectMapper; @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { logger.info("登录失败"); /** * 判断登录方式 */ // 改下面的代码,因为在浏览器访问,所以走了下面else的逻辑,可能老师没有讲, // 我的改法是暂时把else里面也返回为JSON if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())) { response.setContentType("application/json;charset=UTF-8"); // 设置状态码为500 response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); // 这里面返回的包含 MyUserDetailsService 里面的返回对象的值,如是否禁用,过期等 response.getWriter().write(objectMapper.writeValueAsString(new SimpleResponse(exception.getMessage()))); } else { // 记得改这里了额,老师这里没讲 response.setContentType("application/json;charset=UTF-8"); // 设置状态码为500 response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); response.getWriter().write(objectMapper.writeValueAsString(new SimpleResponse(exception.getMessage()))); // super.onAuthenticationFailure(request, response, exception); } } }
题外话:ImoocAuthenctiationSuccessHandler 这里面也是老师没有讲,,,一样的改法,我没有改,,,,你们可以改一下
@Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { logger.info("登录成功"); /** * 判断登录类型 */ if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())) { response.setContentType("application/json;charset=UTF-8"); // 这里面返回的包含 MyUserDetailsService 里面的返回对象的值,如是否禁用,过期等 response.getWriter().write(objectMapper.writeValueAsString(authentication)); } else { super.onAuthenticationSuccess(request, response, authentication); } }
322019-11-07
Spring Security技术栈开发企业级认证与授权
Spring Security技术栈,REST风格开发常见接口,独立开发认证授权模块保证REST服务安全
2662 学习 · 1561 问题
相似问题