除了使用拦截器、过滤器实现对没有权限访问的页面跳转到登陆页外,还可以通过框架实现: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的更多相关文章

  1. spring boot+freemarker+spring security标签权限判断

    spring boot+freemarker+spring security标签权限判断 SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签 ...

  2. 一:Spring Boot、Spring Cloud

    上次写了一篇文章叫Spring Cloud在国内中小型公司能用起来吗?介绍了Spring Cloud是否能在中小公司使用起来,这篇文章是它的姊妹篇.其实我们在这条路上已经走了一年多,从16年初到现在. ...

  3. 基于Spring Boot、Spring Cloud、Docker的微服务系统架构实践

    由于最近公司业务需要,需要搭建基于Spring Cloud的微服务系统.遍访各大搜索引擎,发现国内资料少之又少,也难怪,国内Dubbo正统治着天下.但是,一个技术总有它的瓶颈,Dubbo也有它捉襟见肘 ...

  4. spring boot与spring mvc的区别是什么?

    Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等.但他们的基础都是Spring 的 ioc和 aop ioc 提供了依赖注入的容器 aop ,解决了面向横切面 ...

  5. spring boot与spring mvc的区别

    Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等.但他们的基础都是Spring 的 ioc和 aop ioc 提供了依赖注入的容器 aop ,解决了面向横切面 ...

  6. 基于Spring Boot和Spring Cloud实现微服务架构学习

    转载自:http://blog.csdn.net/enweitech/article/details/52582918 看了几周Spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习感 ...

  7. 基于Spring Boot和Spring Cloud实现微服务架构学习--转

    原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...

  8. 基于Spring Boot和Spring Cloud实现微服务架构

    官网的技术导读真的描述的很详细,虽然对于我们看英文很费劲,但如果英文不是很差,请选择沉下心去读,你一定能收获好多.我的学习是先从Spring boot开始的,然后接触到微服务架构,当然,这一切最大的启 ...

  9. Spring Boot 结合Spring Data结合小项目(增,删,查,模糊查询,分页,排序)

    本次做的小项目是类似于,公司发布招聘信息,因此有俩个表,一个公司表,一个招聘信息表,俩个表是一对多的关系 项目整体结构: Spring Boot和Spring Data结合的资源文件 applicat ...

随机推荐

  1. Hibernate连接池断开自动重连

    异常: javax.servlet.ServletException: org.springframework.transaction.CannotCreateTransactionException ...

  2. 【原创】基于NodeJS Express框架开发的一个VIP视频网站项目及源码分享

    项目名称:视频网站项目 开发语言:HTML,CSS(前端),JavaScript,NODEJS(expres)(后台) 数据库:MySQL 开发环境:Win7,Webstorm 上线部署环境:Linu ...

  3. Navicat premium连接Oracle报ORA-12514错误

    1:ORA-12514 原因:Service Name/SID中的值填的有问题,默认的是ORCL,这个值如果在安装Oracle的时候填的不是ORCL,那就会出现这个错误 解决方法:找到Oracle安装 ...

  4. HDU 5442 Favorite Donut

    Favorite Donut Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  5. 积木大赛 2013年NOIP全国联赛提高组

    题目描述 Description 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为 n 的大厦,大厦可以看成由 n 块宽度为1的积木组成,第i块积木的最终高度需要是hi.在搭建开 ...

  6. LSB、MSB是什么单位

    最低有效位 (LSB: Least Significant Bit)   最低有效位(LSB)是给这些单元值的一个二进制整数位位置,就是,决定是否这个数字是偶数或奇数.LSB有时候是指最右边的位,因为 ...

  7. 20180620关于使用xtrabackup热还原数据库

    参看:http://www.cnblogs.com/waynechou/p/xtrabackup_backup.html http://www.cnblogs.com/waynechou/p/xtra ...

  8. CentOS的free命令

    https://blog.csdn.net/for_tech/article/details/53044610 https://www.58jb.com/html/145.html

  9. iOS:如何让xib同时兼容支持iOS6和iOS7

    做法如下: 1. 取消xib的Use Autolayout属性的勾选: 2. 将xib中每一个子控件(没错,每一个控件,包括UIButton.UILabel等等),将它们的Y Frame的值全部增加2 ...

  10. HDU 4519

    实现简单,但不得不说是一道好题. 当员工数少于医生数时,直接输出K,因为此时N个员工同时检查,必定是最少的时间了. 当员工数大于医生数时,可以把员工的项目看成一段一段的,每个医生对其进行切割,总能得到 ...