【问题标题】:Spring security OAuth authentication requires ClientId and SecretSpring security OAuth认证需要ClientId和Secret
【发布时间】:2018-11-23 05:57:16
【问题描述】:

在我的 Spring Security 应用程序中,我能够使用邮递员从我的“/oauth/token”端点成功获取 JWT 令牌。但是,我希望任何希望进行身份验证的人都可以访问 /oauth/endpoint,而无需 clientId 和 secret。

我的安全服务器

@Configuration
@EnableWebSecurity
public class GatewaySecurityConfigurer extends WebSecurityConfigurerAdapter {

    @Autowired
    private GatewayUserDetailsService gatewayUserDetailsService;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

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

    @Bean
    public DaoAuthenticationProvider daoAuthenticationProvider() {
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        daoAuthenticationProvider.setUserDetailsService(gatewayUserDetailsService);
        return daoAuthenticationProvider;
    }

    @Override
    public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) {
        authenticationManagerBuilder.authenticationProvider(daoAuthenticationProvider());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/oauth/token").permitAll()
                .antMatchers("actuator/**").fullyAuthenticated();
    }

}

和我的资源服务器

@EnableResourceServer
@Configuration
@Import(GatewaySecurityConfigurer.class)
public class GatewayResourceServerConfigurer extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(ResourceServerSecurityConfigurer config) {
        config.tokenServices(tokenServices());
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/oauth/token").permitAll()
                .antMatchers("actuator/**").fullyAuthenticated();
    }

    @Bean
    public TokenStore tokenStore() {
        TokenStore store = new JwtTokenStore(accessTokenConverter());
        return store;
    }

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey("123");
        return converter;
    }

    @Bean
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        return defaultTokenServices;
    }

}

正如您在这两个类中看到的那样,我尝试实现一个 HttpSecurity 配置,该配置将允许对 /oauth/token 的所有请求,这些请求具有需要验证的其他路径。但是,当我在没有 clientId 和秘密的情况下发布时,我的邮递员请求中出现了 401 空 http 响应。

我怎样才能让我的代码不需要 clientId 和 Secret 进行身份验证?

【问题讨论】:

    标签: java spring authentication spring-security oauth


    【解决方案1】:

    OAuth 是一个授权框架。 (参见 RFC 6749。)它允许第三方应用程序(客户端)代表用户获得对 HTTP 服务的有限访问。没有客户端的授权是没有意义的。

    您可能只需要一个简单的端点,用户可以在其中登录并获取 JWT。

    以下教程解释了如何使用自定义 Spring 安全过滤器构建 JWT 身份验证:https://auth0.com/blog/securing-spring-boot-with-jwts

    【讨论】:

      猜你喜欢
      • 2016-07-06
      • 2017-09-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 2016-11-11
      • 2012-10-15
      • 1970-01-01
      • 2015-03-26
      相关资源
      最近更新 更多