使用oAuth2/oAuth/Token请求405方法的Spring安全性不允许
我将oAuth2令牌与Spring Security一起使用。如果我使用与Spring boot 1.3.0相同的配置,它对我来说运行良好。但当我使用与SpringMVC应用程序相同的配置时。然后它会制造一个问题 /oAuth/令牌-->Post 405方法不允许 我的oAuth配置如下:使用oAuth2/oAuth/Token请求405方法的Spring安全性不允许,spring,spring-mvc,spring-security,oauth-2.0,spring-oauth2,Spring,Spring Mvc,Spring Security,Oauth 2.0,Spring Oauth2,我将oAuth2令牌与Spring Security一起使用。如果我使用与Spring boot 1.3.0相同的配置,它对我来说运行良好。但当我使用与SpringMVC应用程序相同的配置时。然后它会制造一个问题 /oAuth/令牌-->Post 405方法不允许 我的oAuth配置如下: import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
@Configuration
public class OAuth2ServerConfiguration {
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends
ResourceServerConfigurerAdapter {
@Autowired
private HttpUnauthorizedEntryPoint authenticationEntryPoint;
@Override
public void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.csrf()
.disable()
.headers()
.frameOptions().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers("/webhook/**").permitAll()
.antMatchers("/app/**").permitAll()
.antMatchers("/api/**").authenticated()
.antMatchers("/protected/**").authenticated();
}
}
@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends
AuthorizationServerConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource);
}
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
endpoints.tokenStore(tokenStore()).authenticationManager(
authenticationManager);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer)
throws Exception {
oauthServer.allowFormAuthenticationForClients();
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient(Constants.htgappClientId)
.scopes("read", "write")
.authorities("ROLE_ADMIN", "ROLE_USER")
.authorizedGrantTypes("password", "refresh_token", "authorization_code", "implicit")
.secret(Constants.htgappClientSecret)
.accessTokenValiditySeconds(Constants.tokenValidityInSeconds);
}
}
}
任何人都能帮我解决问题。您可以在配置中指定允许的方法,如下所示:
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager);
endpoints.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
}
默认情况下,只允许对
/oauth/token端点进行POST。因此,为了允许GET方法,我们必须配置REST端点。仅使用XML配置无法配置允许的令牌端点方法。因此,创建一个额外的配置类,该类将在XML运行后运行@PostConstruct方法,以完成作业
@Configuration
public class OauthTokenEndPointMethodConfig {
@Autowired
private TokenEndpoint tokenEndpoint;
@PostConstruct
public void reconfigure() {
Set<HttpMethod> allowedMethods = new HashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.POST));
tokenEndpoint.setAllowedRequestMethods(allowedMethods);
}
}
@配置
公共类OauthTokenEndPointMethodConfig{
@自动连线
私有令牌端点令牌端点;
@施工后
公共无效重新配置(){
Set allowedMethods=newhashset(Arrays.asList(HttpMethod.GET,HttpMethod.POST));
setAllowedRequestMethods(allowedMethods);
}
}