2017-04-03 259 views
2

我在我的索引中有这个。显示登录用户弹簧安全

登录的用户:

它显示“登录的用户:”并没有什么,即使没有用户登录其他in.User肯定是在correctly.The意向日志是有它说:“登录的用户名称:myusername “当我登录它时,没有什么时候我没有登录。 我有这个bean在@Configuration类

@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() 
} 

//指定依赖 依赖性{

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; 

/** 
* Created by danie on 01/04/2017. 
*/ 
@Configuration 
public class ThymeleafConfig { 

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

Templateconfig

  @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); 
} 

回答

0

在你的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()

+0

它为我除去'TemplateConfig' –

+0

随着这一目标的工作起飞后我加入了线springTemplateEngine.addDialect(新SpringSecurityDialect()) ;到templateEngine(),它修复它非常感谢 – Daniel