如何使用sec:authorize属性?

时间:2019-05-18 10:40:27

标签: spring-boot spring-security thymeleaf

我想使用百里香叶安全性,但是它不起作用。 当用户未通过身份验证时,我想隐藏注销链接。 但是百里香叶的安全性无法正常工作。

我尝试:

<html lang="fa" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org" >
 <li><a sec:authorize="!isAuthenticated()" th:href="@{/login}">login</a></li>
              <li><a sec:authorize="isAuthenticated()" th:href="@{/logout}">logout</a></li>
              <li><a sec:authorize="isAuthenticated()"  th:href="@{/register}">register</a></li>
</html>

这是我的pom.xml文件。我使用以下依赖项:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>   
        </dependency>

            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    <dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0.2</version>
</dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>

</dependency>
        <dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

这是我的安全配置类:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity( prePostEnabled = true, securedEnabled =true, jsr250Enabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{
    @Autowired
    private UserSecurityService usersecurityservice;
    private BCryptPasswordEncoder passwordencoder(){
        return SecurityUtility.passwordEncoder();
    }
    @Override
    protected void configure(HttpSecurity   http)throws Exception{
        http
        .authorizeRequests().
    /*  antMatchers("/**").*/
        antMatchers(PUBLIC_MATCHES).
        permitAll()/*.antMatchers(STORE_MATCHES).hasRole("store_user")*/.anyRequest().authenticated();

    http
        .csrf().disable().cors().disable()
        .formLogin().failureUrl("/login?error")
        /*.defaultSuccessUrl("/")*/
        //.successForwardUrl("/register")
        .loginPage("/login").permitAll()
        .and()
        .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
        .logoutSuccessUrl("/?logout").deleteCookies("remember-me").permitAll()
        .and()
        .rememberMe();

    }
    @Autowired
    public void configureGlobal (AuthenticationManagerBuilder auth) throws Exception{
        auth.userDetailsService(usersecurityservice).passwordEncoder(passwordencoder());
    }

}

我该如何解决我的问题? 我搜索了很多东西,但我不知道答案是什么

1 个答案:

答案 0 :(得分:0)

尝试使用此xmlns:sec="http://www.thymeleaf.org/extras/spring-security"而不是xmlns:sec="http://www.thymeleaf.org"。应该可以解决问题。