如何在Spring授权服务器中允许端点?

时间:2019-12-28 16:07:53

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

我有一个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 Boot2。谢谢您的建议。

1 个答案:

答案 0 :(得分:0)

您需要在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)
基本上,您不希望任何人在您的网站上发布信息,因此,仅当用户可以提供表示他正在使用您的网站进行发布的令牌时,才允许发布POST(该令牌由服务器提供)。现在,在许多Web应用程序中,可以禁用它,因为您要从许多位置进行发布...但是请不要忘记网站的安全性。