当从控制器加载模板时,Thymeleaf会尝试再次进行身份验证

时间:2015-01-22 21:46:42

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

我想使用Thymeleaf从控制器渲染导航视图。但是,导航应基于用户的权限,因此我不想将其从Spring Security中排除。因此,我使用了UrlTemplateResolver:

package it.vertyze.platform.web.controllers;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.templateresolver.UrlTemplateResolver;

@Configuration
public class VZMVCConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private SpringTemplateEngine templateEngine;

    @PostConstruct
    public void templateResolverExtension(){
        UrlTemplateResolver urlTemplateResolver = new UrlTemplateResolver();
        urlTemplateResolver.setOrder(20);
        templateEngine.addTemplateResolver(urlTemplateResolver);
    }


}

但是,当我登录我的网站时,该链接甚至无法解决。相反,我被重定向到登录页面。当我禁用安全性时,我得到一个模板未找到错误,因此模板解析器不会呈现控制器的输出,但是当我导航到它时,它会精确呈现我想要的HTML。 这是我的安全配置:

package it.vertyze.platform.web.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebMvcSecurity
public class VZWebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    VZUserDetailsService userDetailsService;

    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception{
        auth.userDetailsService(userDetailsService).passwordEncoder(encoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{
            http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Bean
    public PasswordEncoder encoder(){
        return new BCryptPasswordEncoder();
    }

}

这是我在封闭模板中调用链接的地方:

<div th:replace="${topNavLinks.getHref()}"></div>

0 个答案:

没有答案