说在前面
上一篇我们说到如何在 spring security 中自定义登录处理逻辑,这一篇我们来讲一下如何自定义登录成功后的处理逻辑。先来回顾下默认情况下,登录成功过后spring security 会帮我们做些什么: 未登录的情况下,我们直接访问应用中的资源,页面会自动跳转到登录页;当登录成功后,页面会自动重定向到我登录前请求的 url。
如何更改默认的登录成功后的处理结果
步骤 1
首先复制上一节的项目工程 spring-security-02,重命名为 Spring-security-03。 maven 依赖不需要改变,如下所示:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
步骤2
定义登录成功后的处理类 GoAuthenticationSuccessHandler
/**
* 自定义 登录成功 处理类
*/
[@Component](https://my.oschina.net/u/3907912)
public class GoAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Autowired
private ObjectMapper objectMapper;
/**
* {"code":200,"message":"操作成功","data":"登录成功"}
* [@param](https://my.oschina.net/u/2303379) request
* [@param](https://my.oschina.net/u/2303379) response
* [@param](https://my.oschina.net/u/2303379) authentication
* @throws IOException
* @throws ServletException
*/
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
response.setHeader("Content-Type", "application/json;charset=utf-8");
response.getWriter().print(objectMapper.writeValueAsString(CommonResult.success("登录成功")));
response.getWriter().flush();
}
}
步骤2
在 WebSecurityConfig 配置类中,注入自定义处理类的依赖 :
@Autowired
private GoAuthenticationSuccessHandler successHandler;
@Autowired
private GoAuthenticationFailureHandler failureHandler;
步骤3
在 protected void configure(HttpSecurity http) 方法中,追加如下代码:
// 这些是本来就有的
http.formLogin()
.loginPage("/loginPage.html")// 自定义登录页
.loginProcessingUrl("/form/login")// 自定义登录 action, 名字随便起
// 以下是新增的
.successHandler(successHandler)// 自定义登录成功处理类
.failureHandler(failureHandler);// 自定义登录失败处理类
小结
扩展
步骤1
`
/**
* 自定义 未登录 或者 token 失效 处理类
*/
@Component
public class GoAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Autowired
private ObjectMapper objectMapper;
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
response.getWriter().println(objectMapper.writeValueAsString(CommonResult.unauthorized(authException.getMessage())));
response.getWriter().flush();
}
}
/**
* 自定义没有访问权限处理类
*/
@Component
public class GoAccessDeniedHandler implements AccessDeniedHandler {
@Autowired
private ObjectMapper objectMapper;
/**
* @param request
* @param response
* @param e
* @throws IOException
* @throws ServletException
*
* @return {"code":403,"message":"没有相关权限","data":"Access is denied"}
*/
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
response.setHeader("Content-Type", "application/json;charset=utf-8");
response.getWriter().print(objectMapper.writeValueAsString(CommonResult.forbidden(e.getMessage())));
response.getWriter().flush();
}
}
`
步骤2
`@Autowiredprivate GoAccessDeniedHandler accessDeniedHandler;
@Autowired
private GoAuthenticationEntryPoint entryPoint;
`
步骤3
@Override protected void configure(HttpSecurity http) throws Exception { // 此处省略一部分代码 http.exceptionHandling() .accessDeniedHandler(accessDeniedHandler)// 用户没有访问权限处理器 .authenticationEntryPoint(entryPoint);// 用户没有登录处理器 }
总结
上面我们总共定义了四个处理类,分别作用于以下四种情况发生之后:
- 当我们登录成功后。
- 当我们登录失败后。
- 当我们没有登录而去访问资源时。
- 当我们访问没有权限的资源时。
我们可以根据实际情况选择性去定义这些处理类,根据具体需求去定义处理逻辑。详细代码可以参考 https://github.com/nimo10050/spring-security-sample/tree/master/spring-security-03