【发布时间】:2019-08-25 22:06:23
【问题描述】:
我有两种类型的网址,一种是安全的,一种是不安全的,例如注册和登录
我希望“注册”和“登录”绕过安全和过滤器,而所有其他 url 必须通过过滤器和安全。
以下是我的安全配置代码,但它不起作用。
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
@Configuration
public class AppSecurity extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailsService userDetailsService;
@Autowired
TempTokenGenerator tempTokenGenerator;
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(getPasswordEncoder());
}
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("notsecured/signin");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests()
.antMatchers("/", "**secured/**").authenticated()
.and().logout().permitAll()
.and()
.apply(new TempConfigurer(tempTokenGenerator));
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
}
我错过了什么?我应该怎么做才能在身份验证和过滤器中包含“安全”网址,同时从身份验证和过滤器中排除“不安全”。
web.ignoring().antMatchers("notsecured/signin");
如果我放的话似乎不起作用
.anyRequest().authenticated()
与
http.authorizeRequests() to make secured urls work.
如果我放了
.antMatchers("/","**/secured/**").authenticated()
与
.anyRequest().permitAll()
它也不起作用。
【问题讨论】:
-
http.authorizeRequests().antMatchers("/unsecured").permitAll() 你期待这个吗?
-
@Sankar 是的,我想要这个用于 /unsecured 但同时我想要 http.authorizeRequests().antMatchers("/secured").authenticated.and() .apply(new TempConfigurer(tempTokenGenerator ));为/安全
-
http.authorizeRequests().antMatchers("/unsecured").permitAll() .antMatchers("/secured/**").hasRole("USER_ROLE") 可以这样使用。
标签: spring-boot security authentication spring-security