【问题标题】:How can I configure SpringBoot with Thymeleaf and use sec:authentication tag如何使用 Thymeleaf 配置 Spring Boot 并使用 sec:authentication 标签
【发布时间】:2015-10-14 14:34:11
【问题描述】:

我正在为我的应用程序使用 spring-boot 1.2.5 + thymeleaf + spring security。

我需要在我的网站上显示用户名,在做了一些研究之后似乎我应该使用类似的代码:

<div sec:authentication="name">The value of the "name" property of
        the authentication object should appear here.</div>

但是我没有让 Thymeleaf 解析该标签。请问我需要一些帮助:(

【问题讨论】:

    标签: spring-security spring-boot thymeleaf


    【解决方案1】:

    如果您正在使用 Spring Boot,并且希望使用 sec:authenticationsec:authorize,请不要忘记在 pom.xml 中包含依赖项

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity3</artifactId>
    </dependency>
    

    【讨论】:

      【解决方案2】:

      你必须做三件事:

      注册组件:

      @Bean
      public SpringTemplateEngine templateEngine(TemplateResolver templateResolver) {
      SpringTemplateEngine templateEngine = new SpringTemplateEngine();
      templateEngine.setTemplateResolver(templateResolver);
      templateEngine.addDialect(new SpringSecurityDialect());
      return templateEngine;
      }
      

      添加百里香安全定义

          <!DOCTYPE html>
      <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:th="http://www.thymeleaf.org"
          xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
      

      最后添加到你的 gradle repo:

      compile("org.thymeleaf:thymeleaf-spring4")
      compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity4")
      

      【讨论】:

        【解决方案3】:

        您需要在项目中添加 Spring Security 3 集成模块。这些模块是等效于 Spring 安全标签库的 Thymeleaf 方言。这些是 thymeleaf 额外模块,不是 Thymeleaf 核心的一部分。

        在您的模板引擎中只需添加集成模块方言。

        <bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
         ...
          <property name="additionalDialects">
            <set>
              <bean class="org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect"/>
            </set>
          </property>
          ...
        </bean>
        

        添加模块后,您可以使用以下代码:

        <div sec:authentication="name">The value of the "name" property of
            the authentication object should appear here.</div>
        

        Thymeleaf 等效模块可以在here 找到。 另外,请参考Step by Step tutorial of TheymeLeaf

        更新:

        如果您使用的是 Spring Boot,您只需在 pom.xml 中添加依赖项或在您的项目中添加 jars。

        【讨论】:

        • 你可以做得比只提供一个链接更好,详细一点(例如在链接旁边添加一个示例)。
        • 我了解,但这不是代码相关的问题。需要在项目中添加模块(或罐子),我已经在这里提到过。我提供了教程的链接以防万一。
        • 是的,我知道,但是声明哪些模块不只是提供一个链接,让答案更具表现力。目前我会将其标记为仅链接答案...
        • 编辑了我的答案。如果你仍然面临这个问题。请提供模板。
        • 实际上他正在使用 Spring Boot,所以他唯一需要做的就是为百里香叶安全 jar 添加一个依赖项,仅此而已。 Spring Boot 会自动为他注册方言。
        【解决方案4】:

        xmlns:sec 也有问题。我花了很多时间试图完成这项工作。在我意识到问题是注册新方言之后,我尝试注册它们,但对我没有任何效果。 最后,在我从互联网上收集了一些代码之后 sec:authorize="hasRole('USER') 开始工作。 我做了一个简单的项目,结合了 Maven 依赖项。这是弹簧靴+百里香叶+弹簧安全。

        我有弹簧靴(不是弹簧 3 或 4)+ thymeleaf + spring security,所有这些都可以正常工作。 Config 是基于 java 的,不是 xml。

        链接到项目https://github.com/hackofi/springboot-sec

        pom.xml

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.2.RELEASE</version>
        </parent>
        
        <dependencies>
            <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-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.thymeleaf.extras</groupId>
                <artifactId>thymeleaf-extras-springsecurity4</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
        </dependencies>
        
        <properties>
            <java.version>1.8</java.version>
        </properties>
        
        
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
        

        WebConfig.java

        @Configuration
        @EnableWebMvc
        @EnableConfigurationProperties
        @ComponentScan(basePackages = "cz.vse")
        public class WebConfig extends WebMvcConfigurerAdapter {
        
            @Autowired
            private Environment environment;
        
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/static/**").addResourceLocations("/static/");
            }
        
        
            @Bean
            public TemplateResolver templateResolver() {
                ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
                templateResolver.setPrefix("/WEB-INF/views/");
                templateResolver.setSuffix(".html");
                templateResolver.setTemplateMode("HTML5");
                return templateResolver;
            }
            @Bean
            public SpringTemplateEngine templateEngine(TemplateResolver templateResolver) {
                SpringTemplateEngine templateEngine = new SpringTemplateEngine();
                templateEngine.setTemplateResolver(templateResolver);
                templateEngine.addDialect(new SpringSecurityDialect());
                return templateEngine;
            }
            @Bean
            public SpringTemplateEngine templateEngine() {
                SpringTemplateEngine templateEngine = new SpringTemplateEngine();
                templateEngine.setTemplateResolver(templateResolver());
                return templateEngine;
            }
        
            @Bean
            public ViewResolver viewResolver() {
                ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
                viewResolver.setTemplateEngine(templateEngine());
                viewResolver.setOrder(1);
                return viewResolver;
            }
        }
        

        WebSecurityConfig.java

        @Configuration
        @EnableWebSecurity
        public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http
                        .authorizeRequests()
                        .antMatchers("/", "/home").permitAll()
                        .anyRequest().authenticated()
                        .and()
                        .formLogin()
                        .loginPage("/login")
                        .permitAll()
                        .and()
                        .logout()
                        .permitAll();
            }
        
            @Autowired
            public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
                auth
                        .inMemoryAuthentication()
                        .withUser("user").password("pass").roles("USER").and()
                        .withUser("derp").password("pass").roles("ADMIN");
            }
        }
        

        【讨论】:

          【解决方案5】:

          在Spring Boot中,除了添加编译依赖:

          compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity3:'2.1.3.RELEASE'
          

          我不得不为方言添加一个 bean:

          @Bean
          IDialect springSecurityDialect() {
              new SpringSecurityDialect()
          }
          

          只有这样,Spring Boot 的自动配置才会通过询问所有 IDialect bean 的上下文来选择方言:

          public ThymeleafDefaultConfiguration(
                  Collection<ITemplateResolver> templateResolvers,
                  ObjectProvider<Collection<IDialect>> dialectsProvider) {
              this.templateResolvers = templateResolvers;
              this.dialects = dialectsProvider.getIfAvailable();
          }
          

          【讨论】:

            猜你喜欢
            • 2020-11-11
            • 2018-04-06
            • 1970-01-01
            • 2018-09-24
            • 2015-04-26
            • 1970-01-01
            • 2016-09-05
            • 2017-06-01
            • 2012-06-03
            相关资源
            最近更新 更多