Skip to content

Commit 98c6c85

Browse files
committed
feat: 添加权限不足、未登录、注销的处理逻辑
1 parent ab1bac9 commit 98c6c85

4 files changed

Lines changed: 92 additions & 2 deletions

File tree

simple-framework/src/main/java/top/cadecode/framework/config/SecurityConfig.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected void configure(HttpSecurity http) throws Exception {
5555
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
5656
http.authorizeRequests()
5757
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
58-
.anyRequest().permitAll()
58+
.anyRequest().authenticated()
5959
.and()
6060
.formLogin().permitAll()
6161
.loginProcessingUrl(LOGIN_URL)
@@ -64,7 +64,11 @@ protected void configure(HttpSecurity http) throws Exception {
6464
.and()
6565
.logout().permitAll()
6666
.logoutUrl(LOGOUT_URL)
67-
.logoutSuccessHandler(signOutSuccessHandler);
67+
.logoutSuccessHandler(signOutSuccessHandler)
68+
.and()
69+
.exceptionHandling()
70+
.authenticationEntryPoint(noAuthenticationHandler)
71+
.accessDeniedHandler(noAuthorityHandler);
6872
}
6973

7074
@Override
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package top.cadecode.framework.security;
2+
3+
import org.springframework.security.core.AuthenticationException;
4+
import org.springframework.security.web.AuthenticationEntryPoint;
5+
import org.springframework.stereotype.Component;
6+
import top.cadecode.common.core.response.CommonResponse;
7+
import top.cadecode.common.enums.AuthErrorEnum;
8+
import top.cadecode.common.util.JsonUtil;
9+
import top.cadecode.common.util.WebUtil;
10+
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
14+
/**
15+
* @author Cade Li
16+
* @date 2021/12/11
17+
* @description 未认证处理器
18+
*/
19+
@Component
20+
public class NoAuthenticationHandler implements AuthenticationEntryPoint {
21+
@Override
22+
public void commence(HttpServletRequest request, HttpServletResponse response,
23+
AuthenticationException authException) {
24+
CommonResponse<Object> commonResponse = CommonResponse.of(AuthErrorEnum.TOKEN_NOT_EXIST)
25+
.path(request.getRequestURI());
26+
WebUtil.writeJsonToResponse(response, JsonUtil.objToStr(commonResponse));
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package top.cadecode.framework.security;
2+
3+
import org.springframework.security.access.AccessDeniedException;
4+
import org.springframework.security.web.access.AccessDeniedHandler;
5+
import org.springframework.stereotype.Component;
6+
import top.cadecode.common.core.response.CommonResponse;
7+
import top.cadecode.common.enums.AuthErrorEnum;
8+
import top.cadecode.common.util.JsonUtil;
9+
import top.cadecode.common.util.WebUtil;
10+
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
14+
/**
15+
* @author Cade Li
16+
* @date 2021/12/11
17+
* @description 权限不足处理器
18+
*/
19+
@Component
20+
public class NoAuthorityHandler implements AccessDeniedHandler {
21+
@Override
22+
public void handle(HttpServletRequest request, HttpServletResponse response,
23+
AccessDeniedException accessDeniedException) {
24+
CommonResponse<Object> commonResponse = CommonResponse.of(AuthErrorEnum.TOKEN_NO_AUTHORITY)
25+
.path(request.getRequestURI());
26+
WebUtil.writeJsonToResponse(response, JsonUtil.objToStr(commonResponse));
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package top.cadecode.framework.security;
2+
3+
import org.springframework.security.core.Authentication;
4+
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
5+
import org.springframework.stereotype.Component;
6+
import top.cadecode.common.core.response.CommonResponse;
7+
import top.cadecode.common.core.response.ResponseCode;
8+
import top.cadecode.common.util.JsonUtil;
9+
import top.cadecode.common.util.WebUtil;
10+
import top.cadecode.framework.config.SecurityConfig;
11+
12+
import javax.servlet.http.HttpServletRequest;
13+
import javax.servlet.http.HttpServletResponse;
14+
15+
/**
16+
* @author Cade Li
17+
* @date 2021/12/11
18+
* @description 注销成功处理器
19+
*/
20+
@Component
21+
public class SignOutSuccessHandler implements LogoutSuccessHandler {
22+
23+
@Override
24+
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
25+
Authentication authentication) {
26+
CommonResponse<Object> commonResponse = CommonResponse.of(ResponseCode.SUCCESS)
27+
.path(SecurityConfig.LOGOUT_URL);
28+
WebUtil.writeJsonToResponse(response, JsonUtil.objToStr(commonResponse));
29+
}
30+
}

0 commit comments

Comments
 (0)