我试图使用多个入口点(取决于用户)来保护我的spring引导应用程序。
我有三种类型的认证:
后
这样做的目的是让/register、/login和/token端点在OAuth身份验证之后可以访问。对于系统用户,只有用户名和密码才能访问/register、/login和/token端点。所有其他端点只能使用从/login或/token端点获得的JWT令牌访问。
为此,我设置了3个WebSecurityConfigurer:
@Configuration
@Order(1)
public class BasicWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.antMatcher("/api/v1/system/**")
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
}
@Override
public void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(new BCryptPasswordEncoder());
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}@Configuration
@Order(2)
public class OAuthWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.antMatcher("/api/v1/access/**")
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.oauth2Login();
}
}@Configuration
@Order(3)
public class JwtWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.antMatcher("/api/v1/**")
.authorizeRequests()
.anyRequest()
.authenticated();
http
.addFilterBefore(new JwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}我遇到的问题是OAuth配置。它无法找到以下url
http://localhost:8080/oauth2/authorization/google我认为antMatcher正在隐藏其他端点吗?
有人能帮忙吗?
谢谢
发布于 2021-03-09 12:23:36
SecurityFilterChain是启动来自"/oauth2/authorization/google"的授权请求的工具。
由于"/oauth2/authorization/google"与"/api/v1/system/**"、"/api/v1/access/**"或"/api/v1/**"不匹配,因此不会为该请求调用SecurityFilterChain,这意味着授权请求没有启动。
您可以更改用于授权请求的基本URI,以匹配您为SecurityFilterChain指定的路径(默认为"/oauth2/authorization/{registrationId}")。
http
.antMatcher("/api/v1/access/**")
.authorizeRequests(authorize -> authorize
.anyRequest().authenticated()
)
.oauth2Login(oauth2 -> oauth2
.authorizationEndpoint(ae -> ae
.baseUri("/api/v1/access/oauth2/authorization/{registrationId}")
)
);https://stackoverflow.com/questions/66532322
复制相似问题