【问题标题】:Spring OAUTH2 for an angular web applciation用于 Angular Web 应用程序的 Spring OAUTH2
【发布时间】:2015-12-25 04:41:51
【问题描述】:

我正在尝试为 Web 应用程序实现 OAUTH2,并且需要一个简单的 OAUTH2 设置来保护其余 API。其余 API 之一提供登录功能,其中验证用户凭据(用户名/密码)。我的用例如下:
1. API 的客户端目前是 AngularJS,但未来可能会有其他客户端
2.用户使用用户名和密码登录到应用程序,即UI层(角度)调用登录休息服务,并在成功的身份验证生成访问令牌与到期时间。
3. 客户端使用此生成的访问令牌来使用应用程序中的其他 API

请为上述用例推荐一个简单的 OAUTH2 配置?

【问题讨论】:

    标签: oauth-2.0 spring-security-oauth2


    【解决方案1】:

    Spring OAuth2 带有内置端点,可通过 API 获取 access_token Resource Owner Password Grant

    这里给你一个简单的配置

    @Configuration
    public class OAuth2ServerConfiguration {
    
        private static final String RESOURCE_ID = "tonywebapp-restservice";
    
        @Configuration
        @EnableAuthorizationServer
        protected static class AuthorizationServerConfiguration extends
            AuthorizationServerConfigurerAdapter {
    
            @Autowired
            @Qualifier("authenticationManagerBean")
            private AuthenticationManager authenticationManager;
         
            @Autowired
            private UserApprovalHandler userApprovalHandler;
    
            @Override
            public void configure(AuthorizationServerEndpointsConfigurer endpoints)
                    throws Exception {
              
                endpoints
                        .tokenStore(tokenStore())
                        .userApprovalHandler(userApprovalHandler)
                        .authenticationManager(authenticationManager)
                        .pathMapping("/oauth/error", "/oauth/error.html")
                        .pathMapping("/oauth/confirm_access", "/oauth/confirm_access.html")
                        ;
    
            }
    
            @Override
            public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
                clients
                    .inMemory()
                        .withClient("your_client_id_here")
                            .resourceIds(RESOURCE_ID)
                            .authorizedGrantTypes("password", "refresh_token")
                            .authorities("USER")
                            .scopes("read.some.thing", "write.some.thing", "any.thing.you.want")
                            .resourceIds(RESOURCE_ID)
                            .secret("your_secret_here");
            }
    
            @Bean
            @Primary
            public DefaultTokenServices tokenServices() {
                DefaultTokenServices tokenServices = new DefaultTokenServices();
                tokenServices.setSupportRefreshToken(true);
                tokenServices.setTokenStore(tokenStore ());
                return tokenServices;
            }
         
            @Bean
            public TokenStore tokenStore() {
                return new InMemoryTokenStore();
            }
         
        }
    
    }
    

    但是,建议不要将此赠款用于基于 Web 的应用程序。因为您无法保护您的secret。我正在为我的 AngularJS 网络应用程序使用 implicit 授权类型。

    【讨论】:

    • 您能否提供一个示例代码 sn-p,其中我们对用户凭据进行了自定义身份验证(即使用自定义身份验证服务),并且在成功身份验证后生成具有到期时间的访问令牌。然后,此访问令牌可用于 AngularJS Web 应用程序使用的其他休息端点的后续授权
    猜你喜欢
    • 2016-01-21
    • 2017-08-24
    • 2016-02-07
    • 2018-09-27
    • 2017-03-11
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    相关资源
    最近更新 更多