index界面
<!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"
>
<head th:replace="~{fragments/header :: header}">
</head>
<body>
<div class="container blog-content-container">
<div sec:authorize="isAuthenticated()" >
<p>已有用户登陆</p>
<p>登陆的用户为:<span sec:authentication="name"></span></p>
<p>用户角色为:<span sec:authentication="principal.authorities"></span></p>
</div>
<div sec:authorize="isAnonymous()">
<p>没有用户登陆</p>
</div>
</div><!-- /.container -->
<div th:replace="~{fragments/footer :: footer}">...</div>
</body>
</html>
SecurityConfig.java
package com.shiliangxu.config;
import org.springframework.beans.factory.annotation.Autowired;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/*******************************************************************************************************
* Copyright © 2018 ShiliangXu. HIT .College of Computer Science and Technology. All rights reserved.
* @Package: com.shiliangxu.config
* @author: ShiliangXu
* @date: 2018/2/5 20:12
* @Description: 安全配置类
*******************************************************************************************************/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
/**
* 自定义配置
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/css/**","/js/**", "/fonts/**", "/index").permitAll() // 都可以访问
.antMatchers("/users/**").hasRole("ADMIN") // 需要相应的角色才能访问
.and()
.formLogin() //基于 Form 表单登录验证
.loginPage("/login").failureUrl("/login-error"); // 自定义登录界面
}
/**
* 认证信息管理
* @param auth
* @throws Exception
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()//认证信息放在内存中
.withUser("xushiliang").password("123456").roles("ADMIN");
}
}
点击登陆之后是可以进入登录界面的,而且如果输入用户名和密码错误的时候,会显示错误,然后重新输入,如果输入正确的话,跳转到的还是上面的界面。既有isAuthenticated的内容又有isAnonymous的内容,但是就是显示当前登录的是谁。
所以页面跳转是没有问题的,我认为还是securityConfig的问题或者是header界面,index的界面的问题,
毕设 Elasticsearch搜索+Thymeleaf模板+JPA+Security+BootStrap
了解课程