Browse Source

modify:

1. 鉴权异常捕获;
tags/24080901
WendyYang 3 months ago
parent
commit
2a6514b063
1 changed files with 20 additions and 1 deletions
  1. +20
    -1
      hz-pm-api/src/main/java/com/hz/pm/api/user/security/config/WebSecurityConfig.java

+ 20
- 1
hz-pm-api/src/main/java/com/hz/pm/api/user/security/config/WebSecurityConfig.java View File

@@ -1,17 +1,23 @@
package com.hz.pm.api.user.security.config;

import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.common.model.constant.CommonConst;
import com.hz.pm.api.user.security.auth.code.AuthCodeLoginSecurityConfig;
import com.hz.pm.api.user.security.auth.credential.CredentialAuthSecurityConfig;
import com.hz.pm.api.user.security.handler.DefaultExpiredSessionStrategy;
import com.hz.pm.api.user.security.handler.DefaultLogoutSuccessHandler;
import com.hz.pm.api.user.security.auth.mh.MhAuthSecurityConfig;
import com.ningdatech.basic.util.NdJsonUtil;
import com.ningdatech.basic.util.StrPool;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

import java.io.PrintWriter;
import java.util.Map;
import java.util.Set;

@@ -38,7 +44,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
assemblerPreAuthUrls(http);
http.formLogin()
.loginPage(authProperties.getAuthRequireUrl())
.and()
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint())
.and().apply(credentialAuthSecurityConfig)
.and().apply(authCodeLoginSecurityConfig)
.and().apply(mhAuthSecurityConfig)
@@ -68,6 +76,17 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.ignoringAntMatchers(authProperties.getIgnoreCsrfUrlsArray());
}

private AuthenticationEntryPoint authenticationEntryPoint() {
return (request, response, authException) -> {
response.setContentType(StrPool.CONTENT_TYPE);
response.setStatus(HttpStatus.UNAUTHORIZED.value());
PrintWriter writer = response.getWriter();
writer.write(NdJsonUtil.getInstance().writeValueAsString(BizConst.UNAUTHENTICATED));
writer.flush();
writer.close();
};
}

private void assemblerPreAuthUrls(HttpSecurity http) throws Exception {
Map<String, String[]> roleArrayMap = authProperties.getRoleArrayMap();
Set<String> roleSet = roleArrayMap.keySet();


Loading…
Cancel
Save