引入依赖
<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.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
配置类SecurityConfig
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/fail").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
http.formLogin().defaultSuccessUrl("/index") //登录认证成功后默认转跳的路径
.failureForwardUrl("/fail");
//http.formLogin().usernameParameter("username").passwordParameter("password").loginPage("/toLogin").loginProcessingUrl("/login"); //.loginPage定制登录跳转页
//http.logout().deleteCookies("remove").invalidateHttpSession(true);
http.csrf().disable(); //关闭csrf功能
http.logout().logoutSuccessUrl("/"); //开启注销功能,注销成功跳转到首页
http.rememberMe().rememberMeParameter("remember"); //开启记住我功能
}
//认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("ls").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2", "vip3")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2", "vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2");
}
}
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css">
</head>
<body>
<div>
<div class="ui container">
<div class="ui segment" id="index-header-nav" th:fragment="nav-menu">
<div class="ui secondary menu">
<a th:href="@{/index}" class="item">首页</a>
<div class="right menu">
<div sec:authorize="!isAuthenticated()">
<!--自定义登录-->
<!-- <a th:href="@{/toLogin}" class="item">-->
<!--security默认登陆-->
<a th:href="@{/login}" class="item">
<i class="address card icon">登录</i>
</a>
</div>
<div sec:authorize="isAuthenticated()">
<a href="" class="item">
用户名:<span sec:authentication="name"></span>
角色:<span sec:authentication="principal.authorities"></span>
</a>
</div>
<div sec:authorize="isAuthenticated()">
<a th:href="@{/logout}" class="item">
<i class="sign-out icon">注销</i>
</a>
</div>
</div>
</div>
</div>
<div>
<br>
<div class="ui segment three column stackable grid">
<div class="column" sec:authorize="hasRole('vip1')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 1</h5>
<hr>
<div><a th:href="@{/level1/1}"><i class="bullhorn icon"></i>Level-1-1</a></div>
<div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i>Level-1-2</a></div>
<div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i>Level-1-3</a></div>
</div>
</div>
</div>
</div>
<div class="column" sec:authorize="hasRole('vip2')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 2</h5>
<hr>
<div><a th:href="@{/level2/1}"><i class="bullhorn icon"></i>Level-2-1</a></div>
<div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i>Level-2-2</a></div>
<div><a th:href="@{/level2/3}"><i class="bullhorn icon"></i>Level-2-3</a></div>
</div>
</div>
</div>
</div>
<div class="column" sec:authorize="hasRole('vip3')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 3</h5>
<hr>
<div><a th:href="@{/level3/1}"><i class="bullhorn icon"></i>Level-3-1</a></div>
<div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i>Level-3-2</a></div>
<div><a th:href="@{/level3/3}"><i class="bullhorn icon"></i>Level-3-3</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
</script>
</body>
</html>
login.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css">
</head>
<body>
<h1 style="text-align: center;margin-top: 50px">登录</h1>
<div class="ui column very relaxed stackable grid" style="width: 400px;margin: 60px auto">
<div class="column">
<div class="ui form">
<form th:action="@{/login}" method="post">
<div class="field">
<label >Username</label>
<div class="ui left icon input">
<input type="text" placeholder="请输入用户名" name="username">
<i class="user icon"></i>
</div>
</div>
<div class="field">
<label >Password</label>
<div class="ui left icon input">
<input type="password" placeholder="请输入密码" name="password">
<i class="lock icon"></i>
</div>
</div>
<input type="checkbox" name="remember">
<input type="submit" class="ui blue submit button">
</form>
</div>
</div>
</div>
</body>
</html>