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 ...
随机推荐
- 前端领域的BEM到底是什么
前端领域的BEM到底是什么 BEM - Block Element Modfier(块元素编辑器) BEM方法确保每一个参加了同一网站开发项目的人,基于一套代码规范去开发,这样非常有利于团队成员理解彼 ...
- .net 学习视频
http://www.iqiyi.com/a_19rrh9jx9p.html http://www.cnblogs.com/aarond/p/SQLDispatcher.html --读写分离 ht ...
- js 阻止冒泡事件和默认事件
阻止事件冒泡 window.enent ? window.enent.cancelBubble = true : e.stopPropagation() function stopBubble(eve ...
- CentOS6.8 安装python2.7,pip以及yum
由于CentOS6.8里自带的yum所依赖的python是2.6.66版本,但是安装pip至少要求python是2.7版本,因而原有的2.6并不能卸载,又得安装新的2.7.之前安装的时候强制卸载了2. ...
- scrapy框架的日志等级和请求传参, 优化效率
目录 scrapy框架的日志等级和请求传参, 优化效率 Scrapy的日志等级 请求传参 如何提高scripy的爬取效率 scrapy框架的日志等级和请求传参, 优化效率 Scrapy的日志等级 在使 ...
- PAT 1109 Group Photo
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
- 【codeforces 767C】Garland
[题目链接]:http://codeforces.com/contest/767/problem/C [题意] 一棵树; 树上的每个节点都有一个权值; 让你把一棵树切掉两条边; 然后把这棵树分成了3个 ...
- 【转】How Many Boyfriends
如果一个女人遇到不同星座男的概率相同 那么这个女人期望遇到多少个男人就能集齐12个不同星座的男人 我们简化一下问题. 如果只有一个星座,那么期望值为1 如果只有两个星座,那遇到第一个男人后 期望再遇到 ...
- Spring Boot-Starter(九)
说明 在使用非spring boot项目我们集成spring mvc mybatis等框架往往需要大量xml配置, spring 的推出是为了解决项目的复杂度,随着项目的增长,xml配置会越来越臃肿, ...
- LeetCode258——Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...