【问题标题】:Which one is the correct OAuth2 flow哪一个是正确的 OAuth2 流程
【发布时间】:2020-04-24 20:53:03
【问题描述】:

尝试在春季实现 OAuth2。但不知道哪一个是正确的流程?

我保持一个流程@Order(1) in (WebSecurityConfigurerAdapter)

在点击下面的页面时,我会看到默认登录页面,并且我成功登录。 http://localhost:8301/oauth/authorize?client_id=getidfromfacebook&response_type=code&redirect_uri=http://localhost:9191/xyz 重定向到授权页面并在接受后获得代码http://localhost:9191/xyz?code=mkuyG4,这有助于通过curl http://localhost:8301/oauth/token -H"Content-type: application/x-www-form-urlencoded" -d'grant_type=authorization_code&redirect_uri=http://localhost:9191/xyz&code=LJQef7' -u getidfromfacebook:getit获取访问和刷新令牌 我还可以通过curl --location --request POST 'http://localhost:8301/oauth/token?grant_type=refresh_token&client_id=getidfromfacebook&refresh_token=a045acd6-5d66-4db5-a509-4bdadca065e0' -u getidfromfacebook:getit从给定的刷新令牌中获取新的访问令牌

我在这里面临的问题是,使用给定的访问令牌,我无法访问中提到的任何资源 antMatchers("/api/**").authenticated() (ResourceServerConfigurerAdapter)。 就像在邮递员中提供了一个带有Authorization 和值Bearer access-token 的标题或类似curl -H"Authorization: Bearer 1738520f-9f9c-43ef-8f7f-f5886075a7aa" http://localhost:8301/api/users/all/。 请注意,我也可以获取其他 grant_types 的访问令牌并刷新它。但无法通过令牌访问资源。需要注意的是,如果我点击资源 url,我会看到默认登录名并能够访问它。

删除 @Order(1) 的另一个流程。当我尝试通过授权代码流程时,系统抱怨用户需要登录才能请求(auth)代码。因此无法继续,因为没有显示默认登录页面。 但是,我可以继续使用密码授予类型 curl http://localhost:8301/oauth/token -d"grant_type=password&username=username&password=userpassword" -H"Content-type:application/x-www-form-urlencoded; charset=utf-8" -u getidfromfacebook:getit 我还可以通过访问令牌访问资源。

哪种方法是正确的? 为什么我无法使用以前的方法访问资源。

@Configuration
@EnableAuthorizationServer
@AllArgsConstructor                            
public class AuthorizationServerConfigAdapter extends AuthorizationServerConfigurerAdapter {

private final AuthenticationManager authenticationManager;
private final ClientService clientService;
private final UserService userService;

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

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.withClientDetails(clientService);
}

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

/*****************************/

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

/*****************************/

@Configuration
@EnableWebSecurity
@AllArgsConstructor
@Order(1) // Since we have this working as N, Z and R sever.
public class WebSecurityConfigAdapter extends WebSecurityConfigurerAdapter {

private final UserService userService;

@Override
protected void configure(HttpSecurity http) throws Exception {

    //http.csrf().disable();

    http
            .antMatcher("/**")
            .authorizeRequests()
            .antMatchers("/oauth/authorize**", "/login**", "/error**")
            .permitAll()
            .and()
            .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin().permitAll();
}

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

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

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder(BCryptPasswordEncoder.BCryptVersion.$2A);
}

}

【问题讨论】:

    标签: oauth-2.0 spring-security-oauth2


    【解决方案1】:
    @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                .requestMatcher(request -> {
                    String auth = request.getHeader("Authorization");
                    return (auth != null && auth.startsWith("Bearer"));
                })
                .authorizeRequests()
                .antMatchers("/api/**").authenticated()
                .antMatchers("/").permitAll();
        }
    

    【讨论】:

    • 看到一个类似的帖子,上面有。
    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 2020-11-23
    • 2016-09-09
    • 2016-11-25
    • 2015-11-18
    • 1970-01-01
    • 2016-05-19
    • 2015-11-05
    相关资源
    最近更新 更多