仅限春季启动授权吗?

时间:2019-01-26 23:03:23

标签: spring-boot authorization

我的服务器有一个可以通过API密钥访问的API。我使用了教程中的解决方案来通过WebSecurityConfigurerAdapter(如下)实现此目标。

此外,我收到一个JWT,它告诉我某些路由的授权。有些路线不需要授权。

但是我不需要身份验证解决方案(除了API密钥)。

如何使用Spring Boot来实现这一目标?

到目前为止,我发现的所有内容都将身份验证与授权结合在一起,似乎很难解开。


当前API密钥实现:

public class APISecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${godojo.http.auth-token-header-name}")
    private String principalRequestHeader;

    @Value("${godojo.http.auth-token}")
    private String principalRequestValue;

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        APIKeyAuthFilter filter = new APIKeyAuthFilter(principalRequestHeader);

        filter.setAuthenticationManager(new AuthenticationManager() {

            @Override
            public Authentication authenticate(Authentication authentication) throws AuthenticationException {                
                String principal = (String) authentication.getPrincipal();
                if (!principalRequestValue.equals(principal))
                {
                    throw new BadCredentialsException("The API key was not found or not the expected value.");
                }
                authentication.setAuthenticated(true);
                return authentication;
            }
        });

        httpSecurity.
            antMatcher("/**").
            csrf().disable().
            sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).
            and().addFilter(filter).authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll().anyRequest().authenticated();
    }

}

0 个答案:

没有答案
相关问题