Spring Boot (22) Spring Security
除了使用拦截器、过滤器实现对没有权限访问的页面跳转到登陆页外,还可以通过框架实现:Spring Security。
使用Spring Security 完成登陆验证:
1.pom.xml添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.创建spring security的配置类WebSecurityConfig.java
@Configuration
//开启spring security
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
//通过authorizeRequests定义哪些url需要被保护,哪些不被保护
.authorizeRequests()
// / 和 /users/ 可以访问
.antMatchers("/", "/users/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
//需要登陆时 转到的登陆页面
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
} @Autowired
//在内存中创建一个用户,该用户的名称为user1,密码为123 用户角色为Admin
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//从内存中获取
auth
.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder())
.withUser("user1")
.password(new BCryptPasswordEncoder().encode("123"))
.roles("Admin");
}
}
新增登陆请求与页面
LoginController.java
@Controller
public class LoginController { @RequestMapping("/login")
public String login(){
return "login";
} }
resources/templates/login.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example </title>
</head>
<body>
<div th:if="${param.error}">
用户名或密码错
</div>
<div th:if="${param.logout}">
您已注销成功
</div>
<form th:action="@{/login}" method="post">
<div><label> 用户名 : <input type="text" name="username"/> </label></div>
<div><label> 密 码 : <input type="password" name="password"/> </label></div>
<div><input type="submit" value="登录"/></div>
</form>
</body>
</html>
测试:访问 /users/ 没问题 ,访问/users/2 跳转到login.html页 , 输入 user1 pass:123 则登陆成功,在访问/users/2 没有问题。
Spring Boot (22) Spring Security的更多相关文章
- spring boot+freemarker+spring security标签权限判断
spring boot+freemarker+spring security标签权限判断 SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签 ...
- 一:Spring Boot、Spring Cloud
上次写了一篇文章叫Spring Cloud在国内中小型公司能用起来吗?介绍了Spring Cloud是否能在中小公司使用起来,这篇文章是它的姊妹篇.其实我们在这条路上已经走了一年多,从16年初到现在. ...
- 基于Spring Boot、Spring Cloud、Docker的微服务系统架构实践
由于最近公司业务需要,需要搭建基于Spring Cloud的微服务系统.遍访各大搜索引擎,发现国内资料少之又少,也难怪,国内Dubbo正统治着天下.但是,一个技术总有它的瓶颈,Dubbo也有它捉襟见肘 ...
- spring boot与spring mvc的区别是什么?
Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等.但他们的基础都是Spring 的 ioc和 aop ioc 提供了依赖注入的容器 aop ,解决了面向横切面 ...
- spring boot与spring mvc的区别
Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等.但他们的基础都是Spring 的 ioc和 aop ioc 提供了依赖注入的容器 aop ,解决了面向横切面 ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习
转载自:http://blog.csdn.net/enweitech/article/details/52582918 看了几周Spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习感 ...
- 基于Spring Boot和Spring Cloud实现微服务架构学习--转
原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...
- 基于Spring Boot和Spring Cloud实现微服务架构
官网的技术导读真的描述的很详细,虽然对于我们看英文很费劲,但如果英文不是很差,请选择沉下心去读,你一定能收获好多.我的学习是先从Spring boot开始的,然后接触到微服务架构,当然,这一切最大的启 ...
- Spring Boot 结合Spring Data结合小项目(增,删,查,模糊查询,分页,排序)
本次做的小项目是类似于,公司发布招聘信息,因此有俩个表,一个公司表,一个招聘信息表,俩个表是一对多的关系 项目整体结构: Spring Boot和Spring Data结合的资源文件 applicat ...
随机推荐
- python编写webservice接口
1.pip install suds-jurko 2.pip install client #coding=utf-8 from suds.client import Client class Web ...
- kernel memory code learn
mem alloc page Noticeble: 1. there are two kind of page: virtual page, physical page. 2. the page st ...
- uva 133(The Dole Queue UVA - 133)
一道比较难想的模拟题,用了队列等东西,发现还是挺难做的,索性直接看了刘汝佳的代码,发现还是刘汝佳厉害! 代码本身难度并不是很大,主要还是p=(p+n+d-1)%n+1;这一句有些难度,实际上经过自己的 ...
- jdk编译安装及tomcat编译安装
这里我安装的jdk版本为1.8版本,tomcat版本为8.5(请上官网下载) 运维开发技术交流群欢迎大家加入一起学习(QQ:722381733) jdk部署: 1.前往软件所在路径 [root@web ...
- PHP中的几个随机数生成函数
PHP中的几个随机数生成函数 rand() 基于 libc 的随机种子发生器 mt_rand() 基于 Mersenne Twister 算法返回随机整数.它可以产生随机数值的平均速度比 libc 提 ...
- 方便简单的远程控制:putty和WinSCP
记录一下WinSCP和putty的用法. putty:远程cmd窗口,在本机通过命令行操作服务器,并且拿到运行结果.而本机只有连接作用,大大减小了负担. 登陆界面输入ip地址,没有特殊情况,默认选项就 ...
- 【codeforces 510B】Fox And Two Dots
[题目链接]:http://codeforces.com/contest/510/problem/B [题意] 让你在一个二维的方格里面找环; 两个点有相邻的边它们才是相连的; 有环YES,没环NO ...
- elasticsearch match和match_phrase匹配原则(十)
分词测试 分词后倒排索引结果:可以通过http://127.0.0.1:9200/_analyze 测试 { "analyzer":"ik_max_word", ...
- 创建Spark执行环境SparkEnv
SparkDriver 用于提交用户的应用程序, 一.SparkConf 负责SparkContext的配置参数加载, 主要通过ConcurrentHashMap来维护各种`spark.*`的配置属性 ...
- hive计算日期差
首先,hive本身有一个UDF,名字是datediff.我们来看一下这个日期差计算的官方描述,(下面这个是怎么出来的): hive> desc function extended datedif ...