From 3bdbd9d2df709279984f862a0e4e7a3b5d7df24f Mon Sep 17 00:00:00 2001 From: zhangfuping <1337425156@qq.com> Date: Mon, 23 Sep 2024 14:24:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E5=92=8C=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/LoginFailureHandler.java | 35 ++++++++++++++++ .../config/LoginSuccessHandler.java | 31 ++++++++++++++ .../springinit/config/SecurityConfig.java | 11 +++-- .../springinit/utils/AjaxResponse.java | 42 +++++++++++++++++++ .../springinit/utils/GlobalConfig.java | 36 ++++++++++++++++ 5 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cc/bnblogs/springinit/config/LoginFailureHandler.java create mode 100644 src/main/java/cc/bnblogs/springinit/config/LoginSuccessHandler.java create mode 100644 src/main/java/cc/bnblogs/springinit/utils/AjaxResponse.java create mode 100644 src/main/java/cc/bnblogs/springinit/utils/GlobalConfig.java diff --git a/src/main/java/cc/bnblogs/springinit/config/LoginFailureHandler.java b/src/main/java/cc/bnblogs/springinit/config/LoginFailureHandler.java new file mode 100644 index 0000000..7ced000 --- /dev/null +++ b/src/main/java/cc/bnblogs/springinit/config/LoginFailureHandler.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() + ))); + } +} + + + diff --git a/src/main/java/cc/bnblogs/springinit/config/LoginSuccessHandler.java b/src/main/java/cc/bnblogs/springinit/config/LoginSuccessHandler.java new file mode 100644 index 0000000..2293378 --- /dev/null +++ b/src/main/java/cc/bnblogs/springinit/config/LoginSuccessHandler.java @@ -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() + ))); + } +} diff --git a/src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java b/src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java index 42cc1dc..caa8145 100644 --- a/src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java +++ b/src/main/java/cc/bnblogs/springinit/config/SecurityConfig.java @@ -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() diff --git a/src/main/java/cc/bnblogs/springinit/utils/AjaxResponse.java b/src/main/java/cc/bnblogs/springinit/utils/AjaxResponse.java new file mode 100644 index 0000000..eef72f8 --- /dev/null +++ b/src/main/java/cc/bnblogs/springinit/utils/AjaxResponse.java @@ -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); + } +} diff --git a/src/main/java/cc/bnblogs/springinit/utils/GlobalConfig.java b/src/main/java/cc/bnblogs/springinit/utils/GlobalConfig.java new file mode 100644 index 0000000..b63d51d --- /dev/null +++ b/src/main/java/cc/bnblogs/springinit/utils/GlobalConfig.java @@ -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; + } + } + +}