除了使用拦截器、过滤器实现对没有权限访问的页面跳转到登陆页外,还可以通过框架实现:Spring Security。

使用Spring Security 完成登陆验证:

1.pom.xml添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>

2.创建spring security的配置类WebSecurityConfig.java

  1. @Configuration
  2. //开启spring security
  3. @EnableWebSecurity
  4. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  5. @Override
  6. protected void configure(HttpSecurity http) throws Exception {
  7. http
  8. //通过authorizeRequests定义哪些url需要被保护,哪些不被保护
  9. .authorizeRequests()
  10. // / 和 /users/ 可以访问
  11. .antMatchers("/", "/users/").permitAll()
  12. .anyRequest().authenticated()
  13. .and()
  14. .formLogin()
  15. //需要登陆时 转到的登陆页面
  16. .loginPage("/login")
  17. .permitAll()
  18. .and()
  19. .logout()
  20. .permitAll();
  21. }
  22.  
  23. @Autowired
  24. //在内存中创建一个用户,该用户的名称为user1,密码为123 用户角色为Admin
  25. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  26. //从内存中获取
  27. auth
  28. .inMemoryAuthentication()
  29. .passwordEncoder(new BCryptPasswordEncoder())
  30. .withUser("user1")
  31. .password(new BCryptPasswordEncoder().encode("123"))
  32. .roles("Admin");
  33. }
  34. }

新增登陆请求与页面

LoginController.java

  1. @Controller
  2. public class LoginController {
  3.  
  4. @RequestMapping("/login")
  5. public String login(){
  6. return "login";
  7. }
  8.  
  9. }

resources/templates/login.html

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml"
  3. xmlns:th="http://www.thymeleaf.org"
  4. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  5. <head>
  6. <title>Spring Security Example </title>
  7. </head>
  8. <body>
  9. <div th:if="${param.error}">
  10. 用户名或密码错
  11. </div>
  12. <div th:if="${param.logout}">
  13. 您已注销成功
  14. </div>
  15. <form th:action="@{/login}" method="post">
  16. <div><label> 用户名 : <input type="text" name="username"/> </label></div>
  17. <div><label> 密 码 : <input type="password" name="password"/> </label></div>
  18. <div><input type="submit" value="登录"/></div>
  19. </form>
  20. </body>
  21. </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. 前端领域的BEM到底是什么

    前端领域的BEM到底是什么 BEM - Block Element Modfier(块元素编辑器) BEM方法确保每一个参加了同一网站开发项目的人,基于一套代码规范去开发,这样非常有利于团队成员理解彼 ...

  2. .net 学习视频

    http://www.iqiyi.com/a_19rrh9jx9p.html http://www.cnblogs.com/aarond/p/SQLDispatcher.html  --读写分离 ht ...

  3. js 阻止冒泡事件和默认事件

    阻止事件冒泡 window.enent ? window.enent.cancelBubble = true : e.stopPropagation() function stopBubble(eve ...

  4. CentOS6.8 安装python2.7,pip以及yum

    由于CentOS6.8里自带的yum所依赖的python是2.6.66版本,但是安装pip至少要求python是2.7版本,因而原有的2.6并不能卸载,又得安装新的2.7.之前安装的时候强制卸载了2. ...

  5. scrapy框架的日志等级和请求传参, 优化效率

    目录 scrapy框架的日志等级和请求传参, 优化效率 Scrapy的日志等级 请求传参 如何提高scripy的爬取效率 scrapy框架的日志等级和请求传参, 优化效率 Scrapy的日志等级 在使 ...

  6. PAT 1109 Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  7. 【codeforces 767C】Garland

    [题目链接]:http://codeforces.com/contest/767/problem/C [题意] 一棵树; 树上的每个节点都有一个权值; 让你把一棵树切掉两条边; 然后把这棵树分成了3个 ...

  8. 【转】How Many Boyfriends

    如果一个女人遇到不同星座男的概率相同 那么这个女人期望遇到多少个男人就能集齐12个不同星座的男人 我们简化一下问题. 如果只有一个星座,那么期望值为1 如果只有两个星座,那遇到第一个男人后 期望再遇到 ...

  9. Spring Boot-Starter(九)

    说明 在使用非spring boot项目我们集成spring mvc mybatis等框架往往需要大量xml配置, spring 的推出是为了解决项目的复杂度,随着项目的增长,xml配置会越来越臃肿, ...

  10. LeetCode258——Add Digits

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...