显示登录用户spring security

Posted

技术标签:

【中文标题】显示登录用户spring security【英文标题】:Display logged in user spring security 【发布时间】:2017-08-28 19:01:53 【问题描述】:

我的索引中有这个。

<div sec:authorize="isAuthenticated()">
    <p>Logged user: <span sec:authentication="name"></span></p>
</div>

即使没有用户登录,它也会显示“登录用户:”而没有其他任何内容。用户肯定是正确登录的。目的是在我登录时让它说“记录的用户:我的用户名”,而当我没有登录时则什么都没有。 我在 @Configuration 类中有这个 bean

@Bean
public SpringSecurityDialect securityDialect() 
    return new SpringSecurityDialect();

我的 build.gradle

buildscript 
    repositories
        mavenCentral()
    
    dependencies 
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE'
    



apply plugin: 'java'
apply plugin: 'idea'

repositories 
    mavenCentral()


// Specify dependencies
dependencies 

    compile 'org.hashids:hashids:1.0.1'
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
    compile 'org.springframework:spring-orm:4.3.7.RELEASE'
    compile 'org.hibernate:hibernate-core:5.2.9.Final'
    compile 'org.hibernate:hibernate-entitymanager:5.0.6.Final'
    compile 'org.apache.tomcat:tomcat-dbcp:8.0.30'
    compile 'org.springframework.boot:spring-boot-starter-security'
    compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity4:2.1.2.RELEASE'
    compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.11.1.RELEASE'
    runtime 'com.h2database:h2'
    runtime 'javax.transaction:jta:1.1'
    runtime 'org.aspectj:aspectjweaver:1.8.7'
    testCompile 'org.springframework.boot:spring-boot-starter-test'

我的html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"></meta>
    <title>Homepage</title>
</head>
<body>
<div sec:authorize="isAuthenticated()">
    <p>Logged user: <span sec:authentication="name"></span></p>
</div>
<form action="searchProducts" method="get">
    <table>
        <tr>
            <td> What you want to search for :</td>
            <td>
                <input name="searchterm" type="text"/>
                <input type="submit" name="submit" value="search"/>
            </td>
        </tr>
    </table>
</form>
</body>
</html>

Thymeleaf 配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect;

@Configuration
public class ThymeleafConfig 

    @Bean
    public SpringSecurityDialect springSecurityDialect() 
        return new SpringSecurityDialect();
    

模板配置

@Configuration
public class TemplateConfig 

    @Bean
    public SpringResourceTemplateResolver templateResolver() 
        final SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setCacheable(false);
        templateResolver.setPrefix("classpath:/");
        templateResolver.setSuffix(".html");
        return templateResolver;
    

    @Bean
    public SpringTemplateEngine templateEngine() 
        final SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
        springTemplateEngine.addTemplateResolver(templateResolver());
        return springTemplateEngine;
    

    @Bean
    public ThymeleafViewResolver viewResolver() 
        final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setOrder(1);
        return viewResolver;
    

安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter 

    @Autowired
    private UserService userService;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception 
        auth.userDetailsService(userService);
    

【问题讨论】:

【参考方案1】:

在您的 html 中,您必须添加安全命名空间:

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

更新:

丹尼尔还将springTemplateEngine.addDialect(new SpringSecurityDialect());添加到templateEngine()

【讨论】:

为了解决这个问题,我添加了行 springTemplateEngine.addDialect(new SpringSecurityDialect());到 templateEngine() 并修复它非常感谢

以上是关于显示登录用户spring security的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 仅针对没有 thymeleaf 登录的用户显示注销按钮

grails spring安全登录页面显示

Spring Security - 显示特定于登录用户的内容

sitemesh + spring security:在主装饰页面中显示登录用户!

spring security为不同用户显示各自的登录成功页面

如何使用 Spring Boot Thymeleaf 显示当前登录的用户?