Authored by cille

update: 增加外部权限配置文件配置

... ... @@ -5,7 +5,7 @@
<parent>
<artifactId>begete</artifactId>
<groupId>com.begete</groupId>
<version>3.3.0</version>
<version>1.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
... ...
permission:
excludeUrls:
# 项目
- /api/system/lotto/list
- /api/sun/**
\ No newline at end of file
... ...
... ... @@ -5,7 +5,7 @@
<parent>
<artifactId>begete</artifactId>
<groupId>com.begete</groupId>
<version>3.3.0</version>
<version>1.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
... ...
... ... @@ -5,7 +5,7 @@
<parent>
<artifactId>begete</artifactId>
<groupId>com.begete</groupId>
<version>3.3.0</version>
<version>1.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
... ...
package com.begete.framework.config;
import com.begete.framework.config.properties.PermissionProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
... ... @@ -8,6 +9,7 @@ import org.springframework.security.config.annotation.authentication.builders.Au
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.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
... ... @@ -18,6 +20,9 @@ import com.begete.framework.security.filter.JwtAuthenticationTokenFilter;
import com.begete.framework.security.handle.AuthenticationEntryPointImpl;
import com.begete.framework.security.handle.LogoutSuccessHandlerImpl;
import javax.annotation.Resource;
import java.util.List;
/**
* spring security配置
*
... ... @@ -56,6 +61,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
@Autowired
private CorsFilter corsFilter;
@Resource
private PermissionProperties permissionProperties;
/**
* 解决 无法直接注入 AuthenticationManager
*
... ... @@ -87,7 +95,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception
{
httpSecurity
ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry = httpSecurity
// CSRF禁用,因为不使用session
.csrf().disable()
// 认证失败处理类
... ... @@ -104,7 +112,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
"/**/*.html",
"/**/*.css",
"/**/*.js"
).permitAll()
).permitAll();
expressionInterceptUrlRegistry
.antMatchers("/profile/**").anonymous()
.antMatchers("/common/download**").anonymous()
.antMatchers("/common/download/resource**").anonymous()
... ... @@ -112,9 +122,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
.antMatchers("/swagger-resources/**").anonymous()
.antMatchers("/webjars/**").anonymous()
.antMatchers("/*/api-docs").anonymous()
.antMatchers("/druid/**").anonymous()
.antMatchers("/druid/**").anonymous();
List<String> excludeUrls = permissionProperties.getExcludeUrls();
if(excludeUrls != null)
excludeUrls.stream().forEach(e -> expressionInterceptUrlRegistry.antMatchers(e).anonymous());
// 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated()
expressionInterceptUrlRegistry.anyRequest().authenticated()
.and()
.headers().frameOptions().disable();
httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
... ...
package com.begete.framework.config;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.lang.Nullable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
Properties propertiesFromYaml = loadYamlIntoProperties(resource);
String sourceName = name != null ? name : resource.getResource().getFilename();
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
}
private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
try {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
} catch (IllegalStateException e) {
// for ignoreResourceNotFound
Throwable cause = e.getCause();
if (cause instanceof FileNotFoundException)
throw (FileNotFoundException) e.getCause();
throw e;
}
}
}
... ...
package com.begete.framework.config.properties;
import com.begete.framework.config.YamlPropertySourceFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.List;
/**
* @author: LiYang
* @Email: lyflyyvip@163.com
* @create: 2020-02-02 16:58
* @Description:
**/
@Configuration
@PropertySource(value = {"classpath:config/permission-properties.yml"}, factory = YamlPropertySourceFactory.class)
@ConfigurationProperties("permission")
public class PermissionProperties {
private List<String> excludeUrls;
public List<String> getExcludeUrls() {
return excludeUrls;
}
public void setExcludeUrls(List<String> excludeUrls) {
this.excludeUrls = excludeUrls;
}
}
... ...
... ... @@ -5,7 +5,7 @@
<parent>
<artifactId>begete</artifactId>
<groupId>com.begete</groupId>
<version>3.3.0</version>
<version>1.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
... ...
... ... @@ -5,7 +5,7 @@
<parent>
<artifactId>begete</artifactId>
<groupId>com.begete</groupId>
<version>3.3.0</version>
<version>1.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
... ...
... ... @@ -5,7 +5,7 @@
<parent>
<artifactId>begete</artifactId>
<groupId>com.begete</groupId>
<version>3.3.0</version>
<version>1.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
... ...
... ... @@ -6,13 +6,13 @@
<groupId>com.begete</groupId>
<artifactId>begete</artifactId>
<version>3.3.0</version>
<version>1.0.1</version>
<name>begete</name>
<description>贝斯管理系统</description>
<properties>
<begete.version>3.3.0</begete.version>
<begete.version>1.0.1</begete.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
... ... @@ -255,12 +255,12 @@
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://112.126.101.137/repository/hosted/</url>
<url>http://nexus.begete.com/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://112.126.101.137/repository/maven-snapshots/</url>
<url>http://nexus.begete.com/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
... ...