【Spring Security + OAuth2 + JWT入门到实战】6. 自定义登录失败处理

配置系统参数

hk:
  security:
    browser:
      loginSucess: /index #登录成功跳转
      loginType: JSON #登录成功 失败返回值类型
      loginPage: /signIn.html  #登录页面
      oginFailure: /failure #登录失败跳转

改造BrowserProperties方法

package com.spring.security.properties;

import lombok.Data;

/**
 * 浏览器的属性
 */
@Data
public class BrowserProperties {

    /**
     * 登录页面   默认登录页signIn.html
     */
    private String loginPage = "/signIn.html";

    /**
     * 登录成功
     */
    private String loginSucess = "/index";


    /**
     * 登录失败
     */
    private String loginFailure = "/failure";

    /**
     * 登录成功 失败返回值类型
     */
    private LoginType loginType = LoginType.JSON;
}

创建登录失败Handler

HkAuthenticationFailureHandler和登录成功控制器在同一目录

package com.spring.security.authentication;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.spring.security.properties.LoginType;
import com.spring.security.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 验证故障处理程序
 *
 */
@Component
public class HkAuthenticationFailureHandler implements AuthenticationFailureHandler {

    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    @Autowired
    private SecurityProperties securityProperties;
    @Autowired
    private ObjectMapper mapper;


    /**
     * 在身份验证失败
     *
     * @param request  请求
     * @param response 响应
     * @param e        e
     * @throws IOException      IOException
     * @throws ServletException Servlet异常
     */
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        //判断配置的返回类型
        if (LoginType.JSON.equals(securityProperties.getBrowser().getLoginType())) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(mapper.writeValueAsString(e.getMessage()));
        } else {
            redirectStrategy.sendRedirect(request, response, securityProperties.getBrowser().getLoginFailure());
        }
    }
}

 

改造BrowserSecurityConfig类

package com.spring.security;

import com.spring.security.authentication.HkAuthenticationFailureHandler;
import com.spring.security.authentication.HkAuthenticationSuccessHandler;
import com.spring.security.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired
    private HkAuthenticationSuccessHandler hkAuthenticationSuccessHandler;

    @Autowired
    private HkAuthenticationFailureHandler hkAuthenticationFailureHandler;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //配置
        http
                .formLogin()
                .loginPage("/authentication/require")//登录页面路径
                // 处理登录请求路径
                .loginProcessingUrl("/authentication/form")
                .successHandler(hkAuthenticationSuccessHandler) // 处理登录成功
                .failureHandler(hkAuthenticationFailureHandler) // 处理登录失败
                .and()
                .authorizeRequests() // 授权配置
                //不需要认证的路径
                .antMatchers("/authentication/require","/signIn.html",securityProperties.getBrowser().getLoginPage(),"/failure").permitAll()
                .anyRequest() // 所有请求
                .authenticated() // 都需要认证
                .and().csrf().disable();
    }
}

修改demo项目HelloController类添加failure方法:

    @GetMapping("failure")
    public String failure() {
        return "登录失败跳转";
    }

现在系统配置返回JSON启动项目访问:http://127.0.0.1:8080/index.html

登录失败返回:

 

修改系统配置loginType: REDIRECT

现在系统配置返回REDIRECT启动项目访问:http://127.0.0.1:8080/index.html

登录失败返回:

注意:如果登录失败返回“访问的服务需要身份认证”,需要把/failure加到不需要认证