大佬教程收集整理的这篇文章主要介绍了SpringBoot 1.5.x +安全性+ OAuth2,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,我现在知道了。
@Cleto Gadelha指出了我非常有用的信息。
但是我认为发行说明还不清楚,或者错过了一些信息。除了OAuth2资源过滤器从3变为之外SecurityPropertIEs.ACCESS_OVERRIDE_ORDER
- 1
,重要的信息是默认WebSecurityConfigurerAdapter
顺序为100(源)。
因此,在1.5.x版本之前,OAuth2资源服务器顺序为3,其优先级 WebSecurityConfigurerAdapter
。
在1.5.x版本之后,OAuth2资源服务器的顺序设置为SecurityPropertIEs.ACCESS_OVERRIDE_ORDER - 1
(Integer.max_value - 8
我认为),现在它的优先级肯定比基本顺序WebSecurityConfigurerAdapter
。
这就是为什么从1.4.x迁移到1.5.x后为我显示登录页面的原因
因此,@Order(SecurityPropertIEs.ACCESS_OVERRIDE_ORDER)
在WebSecurityConfigurerAdapter
类上设置更优雅和类似Java的样式解决方案
我有一个具有OAuth2安全性的Spring Boot REST API。
今天,我已将的版本spring-boot-starter-parent
从升级1.4.2
到1.5.2
。
变化完全使我感到困惑。
以前,我可以使用Postman测试我的REST API。当我的访问令牌不正确或我没有特定资源的权限时,服务器响应如下:
{
"error": "access_denied","error_description": "Access is denied"
}
现在,它一直将我重定向到/login
页面&Hellip;当我登录时-它显示了没有任何OAuth2身份验证的资源&Hellip;
security.oauth2.resource.filter-order = 3
此行已关闭,重定向到登录页面。
但是,我的问题是:
我的代码中一些更重要的部分:
pom.xml
<!--- .... -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<!--- .... -->
<spring-security-oauth.version>2.1.0.RELEASE</spring-security-oauth.version>
<!--- .... -->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Monitor features -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<!-- Security + OAuth2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>${spring-security-oauth.version}</version>
</dependency>
<!--- .... -->
application.properties
#other properties
security.oauth2.resource.filter-order = 3
OAuth2.java
public class OAuth2 {
@EnableAuthorizationServer
@Configuration
@ComponentScan
public static class AuthorizationServer extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManagerBean;
@Autowired
private UserDetailsservice userDetailsservice;
@Override
public void configure(ClientDetailsserviceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("trusted_client")
.authorizedGrantTypes("password","refresh_token")
.scopes("read","write");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManagerBean).userDetailsservice(userDetailsservicE);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.allowFormAuthenticationForClients();
}
}
@EnableresourceServer
@Configuration
@ComponentScan
public static class resourceServer extends resourceServerConfigurerAdapter {
@Autowired
private RoleHierarchy roleHierarchy;
private SecurityExpressionHandler<FilterInvocation> webExpressionHandler() {
DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler();
defaultWebSecurityExpressionHandler.setRoleHierarchy(roleHierarchy);
return defaultWebSecurityExpressionHandler;
}
@Override
public void configure(httpSecurity http) throws Exception {
http
.authorizerequests().expressionHandler(webExpressionHandler())
.antMatchers("/api/**").hasRole("DEVELOPER");
}
}
}
安全性
@EnableWebSecurity
@Configuration
@ComponentScan
public class Security extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsservice userDetailsservice;
@Bean
public JpaAccountDetailsservice userDetailsservice(AccountsRepository accountsRepository) {
return new JpaAccountDetailsservice(accountsRepository);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsservice(userDetailsservicE).passwordEncoder(passwordEncoder());
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public passwordEncoder passwordEncoder(){
return new BCryptpasswordEncoder();
}
}
以上是大佬教程为你收集整理的SpringBoot 1.5.x +安全性+ OAuth2全部内容,希望文章能够帮你解决SpringBoot 1.5.x +安全性+ OAuth2所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。