[英]How to enable spring security for particular url
即时通讯使用春季安全,我的配置是
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
authorizeRequests()
.antMatchers("/**").permitAll()
.antMatchers("/admin/**").hasAuthority("ADMIN").anyRequest().authenticated()
.and().csrf().disable().formLogin().loginPage("/adminlogin").failureUrl("/adminlogin?error=true")
.defaultSuccessUrl("/admin/dashboard")
.usernameParameter("email")
.passwordParameter("password")
.and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/adminlogin?logout=true").and().exceptionHandling()
.accessDeniedPage("/accessdenied");
}
现在,我正在尝试实现所有链接都可以访问而没有任何安全性,但是以/ admin / **开头的链接仅允许角色为“ admin”的用户使用。
但现在,它允许所有人使用/ admin / **。
有什么建议么。
我已经尝试过stackoverflow的许多解决方案,即如何修复Spring Security中的角色? 但没有运气。 行为保持不变,它甚至允许/ admin / url公开使用。
为什么不尝试角色?
@Configuration
@EnableWebSecurity
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN");
}
}
参考: http : //www.baeldung.com/spring-security-expressions-basic
http
.csrf().disable()
.httpBasic().and()
.authorizeRequests()
.antMatchers("/admin/**").hasAuthority("ADMIN")
.antMatchers("/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/adminLogin").failureUrl("/adminLogin?error=true")
.defaultSuccessUrl("/admin/dashboard")
.usernameParameter("email")
.passwordParameter("password")
.and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/adminLogin?logout=true").and().exceptionHandling()
.accessDeniedPage("/accessdenied");
非常适合我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.