【问题标题】:How to configure thymeleaf-extras-springsecurity4 without xml?如何在没有 xml 的情况下配置 thymeleaf-extras-springsecurity4?
【发布时间】:2017-03-19 15:47:17
【问题描述】:

我正在尝试在我的视图中使用类似此代码 sn-p 的代码,但是无论用户的角色如何,内容都会始终显示。

<div sec:authorize="hasRole('ROLE_ADMIN')">            
<!-- Some admin content -->
</div>

【问题讨论】:

    标签: spring-mvc gradle spring-security spring-boot thymeleaf


    【解决方案1】:

    将以下依赖项添加到您的build.gradle

    compile("org.springframework.boot:spring-boot-starter-security")
    

    您还必须添加 Spring Security 配置,如示例:

    @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"));
        }
    }
    

    Securing a Web Application了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-05
      • 2014-01-20
      • 2014-09-01
      • 2019-03-24
      • 2014-02-18
      • 2015-12-09
      • 1970-01-01
      相关资源
      最近更新 更多