mirror of
https://github.com/FatttSnake/Pinnacle-OA.git
synced 2026-04-05 15:01:23 +08:00
Added login failure handler
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.cfive.pinnacle.config;
|
package com.cfive.pinnacle.config;
|
||||||
|
|
||||||
import com.cfive.pinnacle.filter.JwtAuthenticationTokenFilter;
|
import com.cfive.pinnacle.filter.JwtAuthenticationTokenFilter;
|
||||||
|
import com.cfive.pinnacle.handler.AccessDeniedHandler;
|
||||||
import com.cfive.pinnacle.handler.AuthenticationEntryPointHandler;
|
import com.cfive.pinnacle.handler.AuthenticationEntryPointHandler;
|
||||||
import com.cfive.pinnacle.service.permission.impl.UserDetailsServiceImpl;
|
import com.cfive.pinnacle.service.permission.impl.UserDetailsServiceImpl;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -25,6 +26,7 @@ public class SecurityConfig {
|
|||||||
private UserDetailsServiceImpl userDetailsService;
|
private UserDetailsServiceImpl userDetailsService;
|
||||||
private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
|
private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
|
||||||
private AuthenticationEntryPointHandler authenticationEntryPointHandler;
|
private AuthenticationEntryPointHandler authenticationEntryPointHandler;
|
||||||
|
private AccessDeniedHandler accessDeniedHandler;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void setUserDetailsService(UserDetailsServiceImpl userDetailsService) {
|
public void setUserDetailsService(UserDetailsServiceImpl userDetailsService) {
|
||||||
@@ -41,6 +43,11 @@ public class SecurityConfig {
|
|||||||
this.authenticationEntryPointHandler = authenticationEntryPointHandler;
|
this.authenticationEntryPointHandler = authenticationEntryPointHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public void setAccessDeniedHandler(AccessDeniedHandler accessDeniedHandler) {
|
||||||
|
this.accessDeniedHandler = accessDeniedHandler;
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public PasswordEncoder passwordEncoder() {
|
public PasswordEncoder passwordEncoder() {
|
||||||
return new BCryptPasswordEncoder();
|
return new BCryptPasswordEncoder();
|
||||||
@@ -94,6 +101,7 @@ public class SecurityConfig {
|
|||||||
|
|
||||||
.exceptionHandling()
|
.exceptionHandling()
|
||||||
.authenticationEntryPoint(authenticationEntryPointHandler)
|
.authenticationEntryPoint(authenticationEntryPointHandler)
|
||||||
|
.accessDeniedHandler(accessDeniedHandler)
|
||||||
.and()
|
.and()
|
||||||
|
|
||||||
.cors()
|
.cors()
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ public class ResponseCode {
|
|||||||
public static final int DATABASE_CONNECT_ERROR = 20036;
|
public static final int DATABASE_CONNECT_ERROR = 20036;
|
||||||
|
|
||||||
public static final int UNAUTHORIZED = 30010;
|
public static final int UNAUTHORIZED = 30010;
|
||||||
|
public static final int ACCESS_DENIED = 30030;
|
||||||
|
|
||||||
public static final int SYSTEM_ERROR = 50001;
|
public static final int SYSTEM_ERROR = 50001;
|
||||||
public static final int SYSTEM_TIMEOUT = 50002;
|
public static final int SYSTEM_TIMEOUT = 50002;
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.cfive.pinnacle.handler;
|
||||||
|
|
||||||
|
import com.cfive.pinnacle.entity.common.ResponseCode;
|
||||||
|
import com.cfive.pinnacle.utils.WebUtil;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class AccessDeniedHandler implements org.springframework.security.web.access.AccessDeniedHandler {
|
||||||
|
@Override
|
||||||
|
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
|
||||||
|
String objectResponse = WebUtil.objectResponse(ResponseCode.ACCESS_DENIED, "Access denied", null);
|
||||||
|
WebUtil.renderString(response, objectResponse);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,8 @@ import com.cfive.pinnacle.entity.common.ResponseCode;
|
|||||||
import com.cfive.pinnacle.utils.WebUtil;
|
import com.cfive.pinnacle.utils.WebUtil;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
|
import org.springframework.security.authentication.InsufficientAuthenticationException;
|
||||||
import org.springframework.security.core.AuthenticationException;
|
import org.springframework.security.core.AuthenticationException;
|
||||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -14,7 +16,14 @@ import java.io.IOException;
|
|||||||
public class AuthenticationEntryPointHandler implements AuthenticationEntryPoint {
|
public class AuthenticationEntryPointHandler implements AuthenticationEntryPoint {
|
||||||
@Override
|
@Override
|
||||||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
|
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
|
||||||
String objectResponse = WebUtil.objectResponse(ResponseCode.UNAUTHORIZED, "Unauthorized access", null);
|
String objectResponse;
|
||||||
|
if (authException instanceof BadCredentialsException) {
|
||||||
|
objectResponse = WebUtil.objectResponse(ResponseCode.LOGOUT_FAILED, authException.getMessage(), null);
|
||||||
|
} else if (authException instanceof InsufficientAuthenticationException) {
|
||||||
|
objectResponse = WebUtil.objectResponse(ResponseCode.UNAUTHORIZED, authException.getMessage(), null);
|
||||||
|
} else {
|
||||||
|
objectResponse = WebUtil.objectResponse(ResponseCode.UNAUTHORIZED, authException.getClass().toString() + ": " + authException.getMessage(), null);
|
||||||
|
}
|
||||||
WebUtil.renderString(response, objectResponse);
|
WebUtil.renderString(response, objectResponse);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user