【问题标题】:How to permit endpoints in spring authorization server?如何在春季授权服务器中允许端点?
【发布时间】:2020-04-18 02:52:47
【问题描述】:

我有一个 spring boot oauth2 授权服务器,它将提供和授权令牌。我还想为用户创建提供端点。您能告诉我如何允许未经身份验证的用户使用这些端点吗?我尝试了以下配置:

@Configuration
@EnableAuthorizationServer
@RequiredArgsConstructor
public class AuthorizationConfig extends AuthorizationServerConfigurerAdapter {

    private TokenStore tokenStore = new InMemoryTokenStore();

    private final UserDetailsService userDetailsServiceImpl;

    private final AuthenticationManager authenticationManager;

    private final PasswordEncoder passwordEncoder;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        // TODO persist clients details

        clients.inMemory()
                .withClient("browser")
                .authorizedGrantTypes("refresh_token", "password")
                .scopes("ui");
    }

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

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

和授权服务器配置:

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final UserDetailsService userDetailsServiceImpl;

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

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests(authorizeRequests -> {
                    authorizeRequests
                            .antMatchers(HttpMethod.POST, "/user/**").permitAll()
                            .anyRequest().authenticated();
                });
    }

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

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

这是我想要允许的端点:

@RestController
@RequestMapping(path = "/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping
    public UUID create(@RequestBody UserDto userDto) {
        return userService.create(userDto);
    }
}

通过这些配置,我总是得到响应:

{
    "timestamp": "2019-12-28T16:01:09.135+0000",
    "status": 403,
    "error": "Forbidden",
    "message": "Forbidden",
    "path": "/user"
}

我正在使用spring boot 2。谢谢您的建议。

【问题讨论】:

    标签: spring spring-boot spring-security-oauth2


    【解决方案1】:

    您需要在 AuthorizationConfig 类中禁用 CSRF。试试这个配置:

    http.authorizeRequests(authorizeRequests -> {
            authorizeRequests
                    .antMatchers(HttpMethod.POST, "/user/**").permitAll()
                    .anyRequest().authenticated();
        }).csrf(csrf -> {
            csrf.disable();
        });
    

    有关 CSRF 的更多信息,请查看此网站:https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF).
    基本上,您不想允许任何人在您的网站上发布信息,因此只有当用户可以提供表明他正在使用您的网站进行发布的令牌(该令牌由您的服务器提供)时,您才允许发布。现在在许多 Web 应用程序中,您可以禁用它,因为您从许多位置发布...但不要忘记您网站的安全性。

    【讨论】:

      猜你喜欢
      • 2011-05-15
      • 2019-04-28
      • 2021-03-31
      • 2015-08-26
      • 2018-09-26
      • 2016-06-05
      • 2013-08-23
      • 1970-01-01
      • 2017-11-08
      相关资源
      最近更新 更多