我试图在我的视图中使用类似于此代码片段的内容,但是无论用户的角色如何,内容总是显示的。
<div sec:authorize="hasRole('ROLE_ADMIN')">
<!-- Some admin content -->
</div>发布于 2016-11-06 21:17:11
在build.gradle中添加以下依赖项:
compile("org.springframework.boot:spring-boot-starter-security")还必须添加Spring安全配置,例如:
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin").password("admin").roles("ADMIN", "USER")
.and().withUser("user").password("user").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/", "/index/**").permitAll()
.and().authorizeRequests().antMatchers("/login", "logout").permitAll()
.and().formLogin().loginPage("/login").defaultSuccessUrl("/").permitAll()
.and().logout()
.deleteCookies("remove")
.invalidateHttpSession(true)
.logoutUrl("/logout")
.logoutSuccessUrl("/logout-success")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
}
}在保护Web应用程序阅读更多内容。
https://stackoverflow.com/questions/40445888
复制相似问题