File tree Expand file tree Collapse file tree
simple-framework/src/main/java/top/cadecode/framework Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments