我有一个带有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"
}

现在它一直将我重定向到/登录页面…当我登录时 – 它显示我的资源而没有任何OAuth2身份验证…

我试图禁用它,我发现了这个神奇的属性:

security.oauth2.resource.filter-order = 3

此行关闭重定向到登录页面.

但是,我的问题是:

>这两个版本在安全方面发生了什么?
>这个“奇怪的”线是唯一有效的修复吗?
>这个登录页面的目的是什么以及它正在使用什么身份验证(我检查了Google Chrome中的请求和响应,我看不到任何访问令牌和oauth2的内容,所以它只使用用户存储库?)

我的代码中一些更重要的部分:

的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");
    }
}
}

Security.java

@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();
}
}

解决方法

好的,我现在明白了.

@Cleto Gadelha向我指出了非常有用的信息.

但是我觉得发行说明很不清楚或遗漏了一些信息.除了OAuth2资源过滤器从3更改为SecurityProperties.ACCESS_OVERRIDE_ORDER – 1之外,关键信息是默认WebSecurityConfigurerAdapter顺序为100 (source).

因此,在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后,我会看到登录页面的原因

因此,更优雅和类似Java的样式解决方案是在WebSecurityConfigurerAdapter类上设置@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)

java – SpringBoot 1.5.x安全性OAuth2的更多相关文章

  1. android – 如何使用ClientID和ClientSecret在Phonegap中使用Angularjs登录Google OAuth2

    我正尝试使用Angularjs(使用IonicFramework)通过GoogleOAuth2从我的Phonegap应用程序登录.目前我正在使用http://phonegap-tips.com/articles/google-api-oauth-with-phonegaps-inappbrowser.html进行登录.但是当我使用Angular-UI-RouterforIonic时,它正在创建非常

  2. android – 允许用户使用他们的Google凭据访问您的应用的最佳方式

    如果您的Android应用程序需要用户注册,并且您希望允许您的用户通过Google登录,那么您将如何处理?.支持Facebook/Twitter登录我问的原因是因为Twitter和Facebook建议您实施“登录…”身份验证过程>ImplementingSigninwithTwitter>FacebookLoginFlowforAndroid这似乎也完全基于OAuth访问令牌.我不知道的任何其他选项允许您使用他们的Google帐户验证用户?usp=drive_web如果没有其他选项且只有一个访问令牌,您可

  3. android – 针对具有Web视图的本机移动应用程序的SSO方法?

    对于初学者来说,OAuth2访问令牌不会自动传播到Web应用程序中的超链接请求,对吧?那么,网页应用程序页面本身是否必须使用JavaScript重新进行此类传播?移动应用程序可以重写请求来解决此问题,但是:>至少在Android上这只适用于GET请求(对吗?)>更重要的是,这假设Web应用程序不需要在普通的浏览器客户端中运行OAuth2不是正确的方法吗?

  4. Android&amp;OAUTH 2.0

    我不能这样做来拯救我的人生!所以,我有一个基于Codeigniter的RESTapi与OAUTH2.0服务器为我自己的验证系统我想要使用它来允许用户“登录”到我的Android应用程序.我在互联网上找不到任何有关这方面的信息.有一些不支持的OAUTH2.0客户端库,例如Leeloo.我的问题是:>OAUTH2.0是新的还是新的?它还是太新的工作.我应该使用OAUTH1吗?.>我应该使用完全不同的技术吗?例如我听说过“xauth”.再次有关这方面的信息似乎相当粗略.这是否足够容易做到自己?

  5. SpringBoot浅析安全管理之OAuth2框架

    安全管理是软件系统必不可少的的功能。根据经典的“墨菲定律”——凡是可能,总会发生。如果系统存在安全隐患,最终必然会出现问题,这篇文章主要介绍了SpringBoot安全管理OAuth2框架的使用

  6. Spring Cloud oauth2 认证服务搭建过程示例

    这篇文章主要为大家介绍了Spring Cloud oauth2 认证服务搭建过程示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  7. SpringBoot整合SpringSecurityOauth2实现鉴权动态权限问题

    这篇文章主要介绍了SpringBoot整合SpringSecurityOauth2实现鉴权-动态权限,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  8. spring cloud 使用oauth2 问题汇总

    这篇文章主要介绍了spring cloud 使用oauth2 问题汇总,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  9. Spring Cloud OAuth2实现自定义token返回格式

    Spring Security OAuth的token返回格式都是默认的,但是往往这个格式是不适配系统。本文将用一个接口优雅的实现 Spring Cloud OAuth2 自定义token返回格式,需要的可以参考一下

  10. php – 使用Google新OAuth2系统的LightOpenID

    我想为我的Web应用程序实现一个OpenID登录系统.我正在使用LightOpenID.我去了谷歌并发现了旧的(有文档记录的在线)OpenID2系统becameold:Important:GooglehasdeprecatedOpenID2.0andwillshutitdownafteramigrationperiod.IfyourappusesOpenID2.0,theuserinfoendpo

随机推荐

  1. ajax简单例子需要springboot框架maven项目构建

    首先先引入相关的JS,ajax需要引入jqurey,那就引入你的项目中,一般在static下,放哪看个人喜欢:开始贴代码前先排个坑,之前js死活引用不上,总是提示404,所以需要自己加映射,写在WebConfig里:好了,接下来就是写html和controller了。我这里叫testFile.html,在body里加上:Controller:

  2. java – Springboot @retryable没有重试

    以下代码未重试.我错过了什么?我已将以下内容添加到pom.xml中.我也试过为@Retryable提供不同的参数组合.谢谢.解决方法对于要发现的方法的@Retryable注释,需要从初始化的上下文中正确调用它.方法是从spring上下文中调用bean还是通过其他方式调用?

  3. java – SpringBoot 1.5.x安全性OAuth2

    我有一个带有OAuth2安全性的SpringBootRESTAPI.今天我将spring-boot-starter-parent的版本从1.4.2升级到1.5.2.变化让我很困惑.之前,我可以使用Postman测试我的RESTAPI.当我的访问令牌不正确或我没有特定资源的权限时,服务器响应如下:现在它一直将我重定向到/登录页面…当我登录时–它显示我的资源而没有任何OAuth2身份验证…>这个登录页面的目的是什么以及它正在使用什么身份验证(我检查了GoogleChrome中的请求和响应,我看不到任何访问令牌

  4. java – SpringBoot @WebMvcTest,自动装配RestTemplateBuilder

    我在测试SpringController时遇到了问题.我在我的测试类中使用注释@WebMvcTest.当我运行测试时,我收到此错误:没有’org.springframework.boot.web.client.RestTemplateBuilder’类型的限定bean我在我的项目中使用RestTemplate用于其他类,所以我在我的主类中定义了一个bean:为了使它工作,我必须以这种方式定义我的r

返回
顶部