From fa705507ba574c857b1667553737d23b1b7ff495 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期四, 12 五月 2022 12:06:31 +0800
Subject: [PATCH] 后端模板代码优化

---
 src/main/resources/code/service/app/src/main/java/com/ks/app/config/WebSecurityConfig.java |   27 +++++++++++++++++++++++++--
 1 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/main/resources/code/service/app/src/main/java/com/ks/app/config/WebSecurityConfig.java b/src/main/resources/code/service/app/src/main/java/com/ks/app/config/WebSecurityConfig.java
index 8b7b314..1109b27 100644
--- a/src/main/resources/code/service/app/src/main/java/com/ks/app/config/WebSecurityConfig.java
+++ b/src/main/resources/code/service/app/src/main/java/com/ks/app/config/WebSecurityConfig.java
@@ -10,6 +10,7 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.context.annotation.Bean;
+import org.springframework.http.HttpStatus;
 import org.springframework.security.authentication.BadCredentialsException;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
@@ -23,11 +24,13 @@
 import org.springframework.security.core.userdetails.UserDetailsService;
 import org.springframework.security.core.userdetails.UsernameNotFoundException;
 import org.springframework.security.crypto.password.PasswordEncoder;
+import org.springframework.security.web.AuthenticationEntryPoint;
 import org.springframework.security.web.authentication.AuthenticationFailureHandler;
 import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
 import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
 import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
 import org.springframework.web.filter.OncePerRequestFilter;
+import org.yeshi.utils.JsonUtil;
 import org.yeshi.utils.StringUtil;
 
 import javax.annotation.Resource;
@@ -49,6 +52,8 @@
     private AdminUserService adminUserService;
 
     private final String LOGIN_PROCESSING_URL = "/admin/api/login";
+
+    private final String LOGIN_PAGE_PATH="/7aed59d33d777100/login.html";
 
     //鍥惧舰楠岃瘉鐮侀厤缃�
     @Bean
@@ -136,7 +141,7 @@
                 .and()
                 .formLogin()
                 //鑷畾涔夌櫥褰曠晫闈�
-                .loginPage("/login.html")
+                .loginPage(LOGIN_PAGE_PATH)
                 //澶勭悊鐧诲綍閫昏緫鐨剈rl
                 .loginProcessingUrl(LOGIN_PROCESSING_URL)
                 //鐧诲綍鎴愬姛鍚庣殑璺宠浆
@@ -170,11 +175,13 @@
             @Override
             public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
                 logger.info("onLogoutSuccess");
+                httpServletResponse.getWriter().print(JsonUtil.loadTrueResult(""));
             }
         })
                 .and()
                 .csrf().disable()
-                .rememberMe().userDetailsService(new MyUserDetailsService());
+                .rememberMe().userDetailsService(new MyUserDetailsService())
+                .and().exceptionHandling().authenticationEntryPoint(new CustomAuthenticationEntryPoint());
         http.addFilterBefore(new VerificationCodeFilter(), UsernamePasswordAuthenticationFilter.class);
     }
 
@@ -235,4 +242,20 @@
         }
     }
 
+
+    //鑷畾涔夋湭鎺堟潈杩斿洖
+    class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
+        @Override
+        public void commence(HttpServletRequest request, HttpServletResponse response,
+                             AuthenticationException authException) throws IOException, ServletException {
+            String url = request.getRequestURI().toString();
+            if (url.contains("/admin/api/")) {
+                response.setStatus(HttpStatus.UNAUTHORIZED.value());
+            } else {
+                response.sendRedirect(LOGIN_PAGE_PATH);
+            }
+        }
+    }
+
+
 }

--
Gitblit v1.8.0