|
|
@@ -1,19 +1,22 @@
|
|
|
package com.shkpr.service.customgateway.core.config;
|
|
|
|
|
|
+import com.shkpr.service.customgateway.core.components.AccessKeyGenerator;
|
|
|
import com.shkpr.service.customgateway.core.constants.ResponseCode;
|
|
|
import com.shkpr.service.customgateway.core.filter.TokenFilter;
|
|
|
import com.shkpr.service.customgateway.core.properties.GlobalProperties;
|
|
|
import com.shkpr.service.customgateway.core.properties.SecurityProperties;
|
|
|
+import com.shkpr.service.customgateway.core.repository.embedded.AccessKeysRepository;
|
|
|
import com.shkpr.service.customgateway.core.utils.ResponseUtil;
|
|
|
import com.shkpr.service.customgateway.core.utils.TokenUtil;
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
-import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
|
-import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
|
+import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
|
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
|
+import org.springframework.security.web.SecurityFilterChain;
|
|
|
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;
|
|
|
|
|
|
/**
|
|
|
@@ -26,7 +29,7 @@ import org.springframework.security.web.authentication.AnonymousAuthenticationFi
|
|
|
@EnableWebSecurity
|
|
|
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
|
|
@EnableConfigurationProperties(SecurityProperties.class)
|
|
|
-public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
+public class SecurityConfig {
|
|
|
final
|
|
|
GlobalProperties globalProperties;
|
|
|
final
|
|
|
@@ -40,15 +43,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
this.tokenUtil = tokenUtil;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * http安全配置
|
|
|
- * {@inheritDoc}
|
|
|
+ * @return 过滤器链配置
|
|
|
*/
|
|
|
- @Override
|
|
|
- protected void configure(HttpSecurity http) throws Exception {
|
|
|
- //权限
|
|
|
- http.authorizeRequests()
|
|
|
+ @Bean
|
|
|
+ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
|
|
+ return http
|
|
|
+ //权限
|
|
|
+ .authorizeRequests()
|
|
|
.antMatchers(securityProperties.getPermitPattern().toArray(new String[0]))
|
|
|
.permitAll()
|
|
|
.anyRequest()
|
|
|
@@ -76,16 +78,26 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
.authenticationEntryPoint((request, response, authenticationException) ->
|
|
|
ResponseUtil.writeResponseCode(response, ResponseCode.STATUS_NOT_LOGGED_IN, globalProperties.getHttpStatusAlready200())
|
|
|
)
|
|
|
- ;
|
|
|
-
|
|
|
+ .and()
|
|
|
+ .build();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * web安全配置
|
|
|
- * {@inheritDoc}
|
|
|
+ * @return web安全配置
|
|
|
*/
|
|
|
- @Override
|
|
|
- public void configure(WebSecurity web) {
|
|
|
+ @Bean
|
|
|
+ public WebSecurityCustomizer webSecurityCustomizer() {
|
|
|
+ return (web) -> {
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * @return 访问密钥生成器
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public AccessKeyGenerator accessKeyGenerator(AccessKeysRepository repository) {
|
|
|
+ return AccessKeyGenerator.builder()
|
|
|
+ .repository(repository)
|
|
|
+ .build();
|
|
|
}
|
|
|
}
|