0

我已经使用 spring-security-saml2 实现了 saml-adfs 服务提供者。SAML-ADFS 身份验证正常进行。身份验证后,我试图将其重定向到登录页面,该页面几乎没有变量,例如根据登录的用户信息动态填充的 UserID。对于 html 页面渲染,我使用的是 spring-boot-started-thymeleaf lib。我浏览了各种文章并完成了以下配置。我所有的 html 文件都存在于 src/main/resources/templates 中。

我收到“圆形视图路径 [着陆],检查您的 ViewResolver 设置!错误。

If I change it to a static html page, which is present in src/main/resources/static folder, then it is loading the content.

Please guide me how I can resolve this issue. I have dependency of spring-boot-starter-web and spring-boot-starter-thymeleaf in my build.gradle



landing.html page

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Landing!</title>
    </head>
    <body>
        <h1>You are logged as <span th:text="${username}">null</span>!</h1>
        <p>
            <a th:href="@{/saml/logout}">Global Logout</a><br/>
            <a th:href="@{/saml/logout?local=true}">Local Logout</a>
        </p>
    </body>
</html>

@Controller
public class LandingController {

    @RequestMapping("/landing")`enter code here`
    public String landing(@CurrentUser User user, Model model) {
        model.addAttribute("username",  user.getUsername());
        return "landing";
    }
4

1 回答 1

0

首先检查您的视图解析器是否配置正确。尝试配置您的 thymeleaf 视图解析器,例如:

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public ClassLoaderTemplateResolver templateResolver() {

        var templateResolver = new ClassLoaderTemplateResolver();

        templateResolver.setPrefix("templates/");
        templateResolver.setCacheable(false);
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCharacterEncoding("UTF-8");

        return templateResolver;
    }

   @Bean
   public SpringTemplateEngine templateEngine() {
       SpringTemplateEngine templateEngine = new SpringTemplateEngine();
       templateEngine.setTemplateResolver(templateResolver());
       templateEngine.setTemplateEngineMessageSource(messageSource());
       return templateEngine;
   }

   @Bean
   public ViewResolver viewResolver() {
       var viewResolver = new ThymeleafViewResolver();   
       viewResolver.setTemplateEngine(templateEngine());
       viewResolver.setCharacterEncoding("UTF-8");
       return viewResolver;
    }
}

这是基本配置,您可以根据项目结构在此配置中更改模板等的位置。

当您提供此配置时,Thymeleafview 解析器将处理解析 uri 以访问应该解决您的问题的 html。

于 2019-05-22T07:07:52.260 回答