【发布时间】:2017-10-22 05:42:08
【问题描述】:
我正在使用 spring boot 来制作 rest api 并使用 spring security oauth2 进行登录身份验证。当我从与 spring boot 集成的角度客户端向 localhost:8080/oauth/token 发送请求时,它工作正常。(运行在同一个本地主机上:8080)。 但是如果我想从在 localhost(xampp) 上运行的客户端登录,那么它会给出错误
更新:我也在 application.java 文件中保存了这段代码
@SpringBootApplication
public class Application {
private static final String[] REQUEST_METHOD_SUPPORTED = { "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD" };
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
CommandLineRunner init() {
return (args) -> {
FileSystemUtils.deleteRecursively(new File(FileUploadController.ROOT));
Files.createDirectory(Paths.get(FileUploadController.ROOT));
};
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("localhost:80")
.allowedMethods("POST", "GET", "PUT", "DELETE")
.allowedHeaders("header1", "header2", "header3")
.exposedHeaders("header1", "header2");
}
};
}
}
OAuth2ServerConfiguration.java:
@Configuration
@EnableWebSecurity
public class OAuth2ServerConfiguration {
private static final String RESOURCE_ID = "restservice";
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends
ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
// @formatter:off
resources
.resourceId(RESOURCE_ID);
// @formatter:on
}
@Order(-1)
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
// @formatter:on
}
}
@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends
AuthorizationServerConfigurerAdapter {
private TokenStore tokenStore = new InMemoryTokenStore();
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Autowired
private CustomUserDetailsService userDetailsService;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
// @formatter:off
endpoints
.tokenStore(this.tokenStore)
.authenticationManager(this.authenticationManager)
.userDetailsService(userDetailsService);
// @formatter:on
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
// @formatter:off
clients
.inMemory()
.withClient("clientapp")
.authorizedGrantTypes("password", "refresh_token")
.authorities("USER")
.scopes("read", "write")
.resourceIds(RESOURCE_ID)
.secret("LMS");
// @formatter:on
}
@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices tokenServices = new DefaultTokenServices();
tokenServices.setSupportRefreshToken(true);
tokenServices.setTokenStore(this.tokenStore);
return tokenServices;
}
}
}
我已经检查并尝试了许多解决方案,但无法弄清楚。请帮助!!!!
【问题讨论】:
-
添加一个CORS过滤器并让OPTIONS绕过过滤器
-
查看更新的问题 plz.. 是否足以绕过?
标签: java angularjs spring spring-mvc oauth