【问题标题】:How to use sec:authorize attribute?如何使用 sec:authorize 属性?
【发布时间】:2019-10-05 11:31:56
【问题描述】:

我想使用百里香安全,但它不起作用。 我想在用户未通过身份验证时隐藏注销链接。 但是百里香安全不起作用。

我试试这个:

<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());
    }

}

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

【问题讨论】:

  • 你有什么问题?
  • 我不想在用户未登录时显示注销链接
  • 你能展示一下编译 HTML 时发生了什么,检查它时会打印什么吗?
  • 当用户未登录时,注销链接显示。但我不希望它显示

标签: spring-boot spring-security thymeleaf


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-06-05
    • 2013-10-04
    • 1970-01-01
    • 2012-06-12
    • 2015-08-16
    • 2017-09-25
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多