Java 未经授权的选项请求
我尝试使用Oauth2身份验证和angular2访问我的Spring Boot应用程序。当我向“oauth/token”发送一个post请求,并使用我的基本身份验证(包括用户名和密码)来获取令牌(在postman中运行良好),我会得到一个未经授权的401。我知道我的浏览器使用OPTIONS方法发送飞行前请求,并且我已经实现了我的安全配置,因此它应该忽略并允许OPTIONS请求,但它不起作用 这是我的安全配置:Java 未经授权的选项请求,java,spring,spring-security,oauth-2.0,cors,Java,Spring,Spring Security,Oauth 2.0,Cors,我尝试使用Oauth2身份验证和angular2访问我的Spring Boot应用程序。当我向“oauth/token”发送一个post请求,并使用我的基本身份验证(包括用户名和密码)来获取令牌(在postman中运行良好),我会得到一个未经授权的401。我知道我的浏览器使用OPTIONS方法发送飞行前请求,并且我已经实现了我的安全配置,因此它应该忽略并允许OPTIONS请求,但它不起作用 这是我的安全配置: @Configuration @EnableGlobalMethodSecurity(
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private ClientDetailsService clientDetailsService;
@Autowired
DataSource dataSource;
@Autowired
public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery(
"select username, password, 1 from users where username = ?")
.authoritiesByUsernameQuery(
"select u.username, r.name from users u, roles r, role_users ru "
+ "where u.username = ? and u.id = ru.users_id and ru.roles_id = r.id ");
auth.inMemoryAuthentication()
.withUser("admin").password("admin").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.anonymous().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(HttpMethod.OPTIONS,"/oauth/token");
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
尤其是最后一个configure方法应该允许我访问api并获取令牌
有什么问题吗?谢谢你的帮助。我发现了问题……问题出在我身上
这个代码没有错。我刚刚启动了错误的服务器(复制项目)。一切正常。尝试删除“anonymous()。disable()”遗憾的是,它不起作用。谢谢您的帮助。无论是否使用anonymous(),它都可以工作。禁用()。我刚刚启动了项目副本的服务器。一切正常