Spring Security与Spring Boot集成

添加依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency> <!-- h2 database测试时使用的数据库 -->
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

3.1 权限管理

基于角色的权限管理

什么是角色?

  • 代表一系列行为或责任的实体
  • 限定了角色能做什么,不能做什么
  • 用户账号往往与角色相关联

RBAC

  • 基于角色的访问控制(Role-Based Access Control)
  • 隐式访问控制
if(user.hasRole("Project Manager")){
// 显示报表按钮
} else {
// 不显示报表按钮
}
  • 显示访问控制(更加灵活)
if(user.isPermitted("projectReport:view:12345")){
// 显示报表按钮
} else {
// 不显示报表按钮
}

权限解决方案框架:

  • Apache Shiro

  • Spring Security

3.2 Spring Security实战

1)后台编码

  • 安全配置类
package com.fei.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; /**
* 安全配置类 Created by zxf on 2019年10月10日
*/
@EnableWebSecurity // 启用注解安全
public class SecurityConfig extends WebSecurityConfigurerAdapter { /**
* 自定义拦截策略
*/
@Override
protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/css/**", "/js/**", "/fonts/**", "/index").permitAll()// 都可以访问
.antMatchers("/users/**").hasRole("ADMIN")// 需要相应的角色才能访问
.and().formLogin()// 基于form表单登录验证
.loginPage("/login").failureUrl("/login-error");// 自定义登录界面
super.configure(http);
} /**
* 认证信息管理
*
* @param auth
* @throws Exception
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication()// 认证信息存储在内存中
.withUser("fei").password("123").roles("ADMIN");
} }
  • 控制器
package com.fei.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; /**
* 处理首页、登录
* Created by zxf on 2019年10月10日
*/
@Controller
public class MainController {
@GetMapping("/")
public String root() {
return "redirect:/index";
} @GetMapping("/index")
public String index() {
return "index";
} @GetMapping("/login")
public String login() {
return "login";
} @GetMapping("/login-error")
public String loginError(Model model) {
model.addAttribute("loginError", true);
model.addAttribute("errorMsg", "登录失败,用户名或密码错误!"); return "login";
}
}

最新文章

  1. Three.Js学习第一天
  2. Linux服务器Cache占用过多内存导致系统内存不足问题的排查解决(续)
  3. Codeforces 118 D. Caesar&#39;s Legions (dp)
  4. BZOJ3681: Arietta
  5. USACO3.44Raucous Rockers
  6. 简单的新闻客户端APP开发(DCloud+thinkphp+scrapy)
  7. iwebshop 改版页面
  8. Ruiy classicsQuotations
  9. iframe 标签自适应高度和宽度
  10. openstack pike 单机 一键安装 shell
  11. source insight 4.0.86.0 安装过程中出现的问题
  12. IDEA 启动项目前的配置--或过程遇到的问题
  13. 使用SpringAOP获取一次请求流经方法的调用次数和调用耗时
  14. @PathVariable 与@RequestParam
  15. Codeforces 817
  16. linux ------ 使用 TFTP 在两个主机之前传输文件
  17. c++ 的makefile文件实例
  18. 手动配置SVN服务
  19. python3学习笔记.4.turtle绘图
  20. PTA——删除重复字符

热门文章

  1. 2018-5 - 凉经 - 乐糖游戏 - PHP 开发实习生
  2. yield(),sleep()以及wait()的区别
  3. 正则表达式——推荐使用 Unicode 编码
  4. Charles抓包过滤的四种方式
  5. 第四周Java作业及总结
  6. python面试题--初级(一)
  7. JAVA poi设置单元格背景颜色
  8. vue点击除了某组件本身的其它地方, 隐藏该组件的方法
  9. 你知道 Java 类是如何被加载的吗?
  10. P1622释放囚犯