首先讲一下,没有用到数据库,然后觉得重要的就是security的配置securityConfig.class,不太会说(好像也不太会用),上图吧,也是学习狂神过来的

项目结构

大致效果

pom.xml

<dependencies>
<!-- thymeleaf整合security -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency> <!-- spring整合security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.1.4.RELEASE</version>
</dependency> <!--thymeleaf模板-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

重点来了SecurityConfig.class

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override //授权
protected void configure(HttpSecurity http) throws Exception {
//首页所有人都可以访问,但是功能页只有对应权限的人才能访问
//请求授权的规则
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3"); /*
没有授权会自动跳转到登录页面 http://localhost:8080/login
定制登录页.loginPage("/toLogin") http://localhost:8080/toLogin
最后,走回 http://localhost:8080/login(因为要认证用户),页面的action动作为th:action="@{/login}"
其对应 http.formLogin().loginPage("/toLogin").loginProcessingUrl("/login")
*/
http.formLogin().loginPage("/toLogin").usernameParameter("name").passwordParameter("pwd").loginProcessingUrl("/login"); //防攻击网站工具
http.csrf().disable(); //关闭csrf功能,登出失败可能的原因 //注销 bug:注销完,应该跳到首页
http.logout().logoutSuccessUrl("/"); //开启记住我的功能,勾上才生效
http.rememberMe().rememberMeParameter("remember-me");
//http.rememberMe(); //cookie 来的,关闭浏览器还存在,默认时间两周
} @Override //认证
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//在内存中认证(因为没有连接数据库啊)
//密码编码 no PasswordEncoder mapped for the id "null"
//在spring security5+ 之后,新增了许多的加密方法
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip3")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip2","vip3")
.and()
.withUser("sajia").password(new BCryptPasswordEncoder().encode("123")).roles("vip1");
}
}

MyController.class

@Controller
public class MyController {
@RequestMapping({"/","/index"})
public String index(){
return "index";
}
@RequestMapping("/hello")
public String hello(){
return "hello springsecurity !";
} @RequestMapping("/toLogin")
public String toLogin(){
return "views/login";
} //9个页面
@RequestMapping("/level1/{id}")
public String level1(@PathVariable("id") String id){
return "views/level1/"+id;
} @RequestMapping("/level2/{id}")
public String level2(@PathVariable("id") String id){
return "/views/level2/"+id;
}
@RequestMapping("/level3/{id}")
public String level3(@PathVariable("id")String id){
return "/views/level3/"+id;
}
}

application.properties

#关闭模板引擎的缓存
spring.thymeleaf.cache=false

首页index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>首页</title>
<!--semantic-ui-->
<link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
<link th:href="@{/qinjiang/css/qinstyle.css}" rel="stylesheet">
</head>
<body> <!--主容器-->
<div class="ui container"> <div class="ui segment" id="index-header-nav" th:fragment="nav-menu">
<div class="ui secondary menu">
<a class="item" th:href="@{/index}">首页</a> <!--登录注销--> <div class="right menu">
<!--未登录-->
<div sec:authorize="!isAuthenticated()">
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登录
</a>
</div> <!--登录了:显示注销,用户名-->
<div sec:authorize="isAuthenticated()">
<a class="item">
用户名:<span sec:authentication="name"></span> |
角 色:<span sec:authentication="principal.authorities"></span>
</a>
</div>
<div sec:authorize="isAuthenticated()">
<a class="item" th:href="@{/logout}">
<i class="sign-out icon"></i> 注销
</a>
</div> </div>
</div>
</div> <div class="ui segment" style="text-align: center">
<h3>Spring Security Study by 秦疆</h3>
</div> <div>
<br>
<div class="ui 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> <script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script>
<script th:src="@{/qinjiang/js/semantic.min.js}"></script> </body>
</html>

login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>登录</title>
<!--semantic-ui-->
<link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
</head>
<body> <!--主容器-->
<div class="ui container"> <div class="ui segment"> <div style="text-align: center">
<h1 class="header">登录</h1>
</div> <div class="ui placeholder segment">
<div class="ui column very relaxed stackable grid">
<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="Username" name="name">
<i class="user icon"></i>
</div>
</div>
<div class="field">
<label>Password</label>
<div class="ui left icon input">
<input type="password" name="pwd">
<i class="lock icon"></i>
</div>
</div>
<!--记住我-->
<div class="field">
<input type="checkbox" id="remember-me" name="remember-me"/>记住我
</div>
<input type="submit" class="ui blue submit button"/>
</form>
</div>
</div>
</div>
</div> <div style="text-align: center">
<div class="ui label">
</i>注册
</div>
<br><br>
<small>blog.kuangstudy.com</small>
</div>
<div class="ui segment" style="text-align: center">
<h3>Spring Security Study by 秦疆</h3>
</div>
</div> </div> <script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script>
<script th:src="@{/qinjiang/js/semantic.min.js}"></script> </body>
</html>

剩下就是头疼的静态资源了

1.html(剩下八个都一样的,改下<h3>Level-1-1</h3>这个就好了)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>首页</title>
<!--semantic-ui-->
<link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
<link th:href="@{/qinjiang/css/qinstyle.css}" rel="stylesheet">
</head>
<body> <!--主容器-->
<div class="ui container"> <div th:replace="~{index::nav-menu}"></div> <div class="ui segment" style="text-align: center">
<h3>Level-1-1</h3>
</div> </div> <script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script>
<script th:src="@{/qinjiang/js/semantic.min.js}"></script> </body>
</html>

qinstyle.css

body{
min-width: 1150px;
overflow: auto;
}
#index-header-nav{
font-size: 0px;
}
#blog-lable>a{
margin: 1px;
}

剩下就是加油,多学多看多动手,加油

springsecurity学习的更多相关文章

  1. SpringSecurity学习之自定义过滤器

    我们系统中的认证场景通常比较复杂,比如说用户被锁定无法登录,限制登录IP等.而SpringSecuriy最基本的是基于用户与密码的形式进行认证,由此可知它的一套验证规范根本无法满足业务需要,因此扩展势 ...

  2. SpringSecurity学习之基于数据库的用户认证

    SpringSecurity给我们提供了一套最基本的认证方式,可是这种方式远远不能满足大多数系统的需求.不过好在SpringSecurity给我们预留了许多可扩展的接口给我们,我们可以基于这些接口实现 ...

  3. SpringSecurity学习之快速上手

    互联网项目中,安全与权限控制是不可回避的问题,为了解决这一些列问题,许多安全框架应运而生了.这些框架旨在帮我们解决公用的安全问题,让我们的程序更加健壮,从而让程序员全身心投入到业务开发当中.那么Spr ...

  4. SpringSecurity学习四----------基于不同角色跳转到不同URL

    © 版权声明:本文为博主原创文章,转载请注明出处 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  5. SpringSecurity学习三----------通过Security标签库简单显示视图

    © 版权声明:本文为博主原创文章,转载请注明出处 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  6. SpringSecurity学习二----------实现自定义登录界面

    © 版权声明:本文为博主原创文章,转载请注明出处 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  7. SpringSecurity学习一----------最简单的权限控制系统

    © 版权声明:本文为博主原创文章,转载请注明出处 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  8. SpringSecurity学习笔记(一):搭建最简单的SpringSecurity应用

    学习过程参考自:http://www.mossle.com/docs/auth/html/pt01-quickstart.html 一.搭建Maven项目: 所需引用的jar包如下: pom.xml文 ...

  9. SpringSecurity学习总结

    第一.SpringSecurity-简介 1.1简介 SpringSecurity融合Spring技术栈,提供JavaEE应 用的整体安全解决方案: Spring Security为基于Java EE ...

随机推荐

  1. [.NET] ConfuserEx脱壳工具打包

    [.NET] ConfuserEx脱壳工具打包 ConfuserEx 1.0.0脱壳步骤        Written by 今夕何夕[W.B.L.E. TeAm] 1.先用UnconfuserEx把 ...

  2. Sublime Text 全局搜索Ctrl+Shift+F快捷键不能用

      Sublime Text 全局搜索Ctrl+Shift+F快捷键不能用   和微软输入法的简繁体切换冲突了,关闭输入法的简繁体切换快捷键就好了! 文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢 ...

  3. ccf 201803-2 碰撞的小球(Python)

    问题描述 数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐标L处.有n个不计体积的小球在线段上,开始时所有的小球都处在偶数坐标上,速度方向向右,速度大小为1单位长度每秒. 当小球到达线 ...

  4. Heat map 绘图神奇

    https://study.163.com/provider/400000000398149/index.htm?share=2&shareId=400000000398149(博主视频教学主 ...

  5. SpringBoot 部署到Linux开机自启动和运行

    前文 SpringBoot是一个强大的微服务框架,通常都是打包项目成Jar包,并部署到服务器上,本文以Linux服务器部署为主 开机自启动 博文:<https://www.cnblogs.com ...

  6. C++ replace replace_if replace_copy replace_copy_if

    #include <iostream>#include <list>#include <algorithm>#include <iterator>#in ...

  7. mac配置maven项目的方法(含导入selenium包)

    1.配置了java的环境变量    文件位置:  /Users/lucax/.bash_profile JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1 ...

  8. Java 实现 ssh命令 登录主机执行shell命令

    Java 实现 ssh命令 登录主机执行shell命令 1.SSH命令 SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定:SS ...

  9. Flutter中的浮动按钮FloatingActionButton 及融合底部工具栏

    FloatingActionButton 简称 FAB,从字面理解可以看出,它是“可交互的浮动按钮”,其实在Flutter默认生成的代码中就有这家伙,只是我们没有正式的接触. 一般来说,它是一个圆形, ...

  10. Json乱码解决统一代码

    我们在springmvc中使用json经常出现乱码格式  如下图: 我们可以在@RequestMapping()中配置,produces = "application/json;charse ...