自定义登录成功和失败的逻辑

master
zhangfuping 2 months ago
parent c9a4b9c5a1
commit 3bdbd9d2df
  1. 35
      src/main/java/cc/bnblogs/springinit/config/LoginFailureHandler.java
  2. 31
      src/main/java/cc/bnblogs/springinit/config/LoginSuccessHandler.java
  3. 11
      src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java
  4. 42
      src/main/java/cc/bnblogs/springinit/utils/AjaxResponse.java
  5. 36
      src/main/java/cc/bnblogs/springinit/utils/GlobalConfig.java

@ -0,0 +1,35 @@
package cc.bnblogs.springinit.config;
import cc.bnblogs.springinit.utils.AjaxResponse;
import cc.bnblogs.springinit.utils.GlobalConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component("loginFailureHandler")
public class LoginFailureHandler implements AuthenticationFailureHandler{
@Autowired
private ObjectMapper objectMapper;
@Override
public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {
httpServletResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
httpServletResponse.setContentType("application/json;charset=UTF-8");
httpServletResponse.getWriter().write(objectMapper.writeValueAsString(
AjaxResponse.AjaxData(GlobalConfig.ResponseCode.ERROR.getCode(),
GlobalConfig.ResponseCode.ERROR.getDesc(),
"登录失败:"+e.getMessage()
)));
}
}

@ -0,0 +1,31 @@
package cc.bnblogs.springinit.config;
import cc.bnblogs.springinit.utils.AjaxResponse;
import cc.bnblogs.springinit.utils.GlobalConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component("loginSuccessHandler")
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
@Autowired
private ObjectMapper objectMapper;
@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
httpServletResponse.setContentType("application/json;charset=UTF-8");
httpServletResponse.getWriter().write(objectMapper.writeValueAsString(
AjaxResponse.AjaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
GlobalConfig.ResponseCode.SUCCESS.getDesc(),
authentication.getName()
)));
}
}

@ -1,7 +1,7 @@
package cc.bnblogs.springinit.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@ -12,8 +12,11 @@ import org.springframework.security.web.SecurityFilterChain;
@EnableWebSecurity
public class SecurityConfig {
@Autowired
private LoginFailureHandler loginFailureHandler;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
public SecurityFilterChain securityFilterChain(HttpSecurity http, LoginSuccessHandler loginSuccessHandler) throws Exception {
http
.csrf().disable()
.authorizeRequests()
@ -21,7 +24,9 @@ public class SecurityConfig {
.anyRequest().authenticated() // 默认所有请求需登录
.and()
.formLogin()
.defaultSuccessUrl("/hello", true) // 登录成功后跳转到 /hello
.successHandler(loginSuccessHandler)
.failureHandler(loginFailureHandler)
// .defaultSuccessUrl("/hello", true) // 登录成功后跳转到 /hello
.permitAll()
.and()
.logout()

@ -0,0 +1,42 @@
package cc.bnblogs.springinit.utils;
public class AjaxResponse {
private Integer status;
private String msg;
private Object obj;
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
private AjaxResponse(Integer status, String msg, Object obj) {
this.status = status;
this.msg = msg;
this.obj = obj;
}
public static AjaxResponse AjaxData(Integer status, String msg, Object obj) {
return new AjaxResponse(status, msg, obj);
}
}

@ -0,0 +1,36 @@
package cc.bnblogs.springinit.utils;
public class GlobalConfig {
/**
* 测试场景
*/
public static final Boolean Test = false;
//windows路径
public static final String BPMN_PathMapping = "file:D:\\WangJianIDEA_Test\\activiti-imooc\\src\\main\\resources\\resources\\bpmn\\";
//Liunx路径
//public static final String BPMN_PathMapping = "file:/root/Activiti/";
public enum ResponseCode {
SUCCESS(0, "成功"),
ERROR(1, "错误");
private final int code;
private final String desc;
ResponseCode(int code, String desc) {
this.code = code;
this.desc = desc;
}
public int getCode() {
return code;
}
public String getDesc() {
return desc;
}
}
}
Loading…
Cancel
Save