【问题标题】:Spring Security - Oauth implementationsSpring Security - Oauth 实现
【发布时间】:2020-03-16 00:42:38
【问题描述】:

我正在尝试实现与 oauth2 类似的授权服务,只是它不需要用户名和密码来生成密钥和刷新令牌。但从我发现它看起来像你有给它一个用户和一个密码。有什么解决方法吗?

我的代码是:

@Configuration
public class AuthorizationServerConfiguration implements AuthorizationServerConfigurer {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.checkTokenAccess("isAuthenticated()").tokenKeyAccess("permitAll()");
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource).passwordEncoder(passwordEncoder);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager);
    }

}


@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;


    @Bean
    public AuthenticationManager getAuthenticationManager() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }
}

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    根据您的要求,您可以使用 JWT Token。 支持RS256、HS256等多种算法

    我会以HS256 作为参考。

    在您的授权服务器中,您需要根据某个 Secret 密钥生成 Token。 在客户端。您需要使用相同的密钥验证令牌。

    如果您不想在授权服务器和客户端服务器之间共享密钥

    您可以使用具有公钥和私钥概念的RS256。

    为了实现,您需要添加一个Filter,它会检查每个调用的标头中的令牌。

    请参阅此帖子以获取与实施相关的more detail。

    【讨论】:

      【解决方案2】:

      如果我理解正确,您想删除服务器到服务器的身份验证,因为您可能不需要它,因为它是一个客户端应用程序。我们不会,至少我还没有找到方法,所以使用一些微不足道的东西并保持原样。如果有人找到了方法,请分享。

      【讨论】:

      • 是的,这就是我想要的
      猜你喜欢
      • 2015-02-27
      • 2013-08-29
      • 2011-04-17
      • 2012-05-04
      • 2013-07-28
      • 2019-02-18
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多