【问题标题】:Spring security oauth2 - Can't access /oauth/token routeSpring security oauth2 - 无法访问 /oauth/token 路由
【发布时间】:2018-07-22 20:18:39
【问题描述】:

我刚刚开始按照本教程为我的 rest api 制作身份验证服务器:https://jugbd.org/2017/09/19/implementing-oauth2-spring-boot-spring-security/。一切都很顺利,直到最后我无法访问 /oauth/token 路由以进行身份​​验证。

我想我需要更多解释才能完全理解此身份验证。

谢谢你, 马蒂厄·穆尼耶

PS:这是我的课程:

ResourceServerConfig.java

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/api/secure/**").authenticated();
    }
}

AuthorizationServerConfig.java

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer serverSecurityConfigurer){
        serverSecurityConfigurer
                .tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()")
                .allowFormAuthenticationForClients();
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception{
        clients.inMemory().withClient("android-client")
                .authorizedGrantTypes("client-credentials", "password", "refresh_token")
                .authorities("ROLE_CLIENT", "ROLE_ANDROID_CLIENT")
                .scopes("read", "write", "trust")
                .resourceIds("oauth2resource")
                .accessTokenValiditySeconds(5000)
                .secret("android-secret").refreshTokenValiditySeconds(50000);
    }
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints){
        endpoints.authenticationManager(authenticationManager)
                .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
    }
}

就在我的主要下方

@Autowired
    CustomUserDetailsService userDetailsService;

    @Autowired
    public void authenticationManager(AuthenticationManagerBuilder builder) throws Exception{
        builder.userDetailsService(userDetailsService);
    }

最后是我的 CustomUserDetailsS​​ervice 和 UserService

CustomUserDetailsS​​ervice.java

@Service
public class CustomUserDetailsService implements UserDetailsService {

    private final UserService userService;

    @Autowired
    public CustomUserDetailsService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        return this.userService.findByEmail(email);
    }
}

用户服务.java

@Service
public class UserService {

    @Autowired
    MembreRepository membreRepository;

    public UserDetails findByEmail(String email){
        return membreRepository.findOneByEmail(email);
    }
}

【问题讨论】:

  • 嗨@dur 我做了一些修改,知道我可以验证自己,但前提是密码没有经过哈希处理。我仍然不明白密码验证是在哪里发出的。您知道如何进行验证吗?
  • 如果我使用数据库中的非散列密码,我可以验证自己,否则我无法确定在验证之前密码在哪里“散列”:/

标签: java spring-security oauth


【解决方案1】:

默认情况下,端点 /oauth/token 是安全的,

因此,要调用此端点,您需要作为客户端进行身份验证。为此,根据您的设置,您需要在 POST 正文上传递 client_id 和 client_secret(您的设置允许客户端身份验证表单.allowFormAuthenticationForClients())。

尝试使用参数调用端点:

网址

{{host}}/oauth/token

标题

Content-Type application/x-www-form-urlencoded

发布参数

grant_type: password
scope: read write
username: foo
password: bar
client_id: android-client
client_secret: android-secret

我用 Postman 测试

【讨论】:

  • 谢谢你,我终于明白了我错过了我的客户 ID 和秘密,结果我不得不把它们放在我的标题中。我会看看它是否也适用于我的请求正文。知道其中一种方法是否比另一种更好吗?
  • 我相信这取决于应用程序的类型(Web 或移动),但我也更喜欢通过 header 使用
猜你喜欢
  • 2013-12-31
  • 2017-08-31
  • 2015-04-26
  • 2016-05-20
  • 1970-01-01
  • 2020-02-08
  • 2018-04-26
  • 2017-08-31
  • 2017-09-25
相关资源
最近更新 更多