Как использовать атрибут sec: authorize? - PullRequest
0 голосов
/ 18 мая 2019

Я хочу использовать защиту от тимелина, но она не работает.Я хочу скрыть ссылку выхода, когда пользователь не авторизуется.Но доза защиты от тимелина не работает.

Я пытаюсь это сделать:

<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 голосов
/ 20 мая 2019

Попробуйте это xmlns:sec="http://www.thymeleaf.org/extras/spring-security" вместо xmlns:sec="http://www.thymeleaf.org". Это должно решить проблему.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...