在web开发中,安全第一位!拦截器、过滤器、安全框架

Spring Security

Spring Security是针对springboot项目的安全框架,也是Spring Boot低层安全模块默认的技术选型,他可以实现强大的web控制,对于安全控制,我们仅需要引入spring-boot-stater-security模块,进行少量的配置来实现强大的安全管理!
记住几个类:

  • WebSecurityConfigureAdapter:自定义Security策略
  • AuthenticationManagerBuilder:自定义认证策略
  • @EnableWebSecurity: 开启WebSecurity模式

Spring Security的两个主要目标是“认证”和“授权”

依赖

  <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>

登录、注销

// aop :拦截器
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {// 链式编程@Overrideprotected void configure(HttpSecurity http) throws Exception {// 请求设置角色http.authorizeRequests().antMatchers("/index").permitAll().antMatchers("/level1/**").hasRole("vip1").antMatchers("/level2/**").hasRole("vip2").antMatchers("/level3/**").hasRole("vip3");//  设置自定义登录界面,// loginPage 登录界面// loginProcessingUrl 登录时所发的请求与登录界面action同步// defaultSuccessUrl 登录成功后的请求http.formLogin().loginPage("/tologin").loginProcessingUrl("/tologin").defaultSuccessUrl("/toindex",true);// 跨站请求伪造 --何为跨站请求伪造自行百度http.csrf().disable();// 注销  // logout Security自带一个/logout的请求,只需将注销请求地址设置为其自带的即可// logoutSuccessUrl 注销后所发请求http.logout().logoutSuccessUrl("/index");// 记住我 cookie 默认两周     默认参数名remember-me可自定义http.rememberMe().rememberMeParameter("rememme");}@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {//方式一:内存  账号设置角色auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("developer").password(new BCryptPasswordEncoder().encode("1")).roles("vip1", "vip2", "vip3").and().withUser("xusx").password(new BCryptPasswordEncoder().encode("1")).roles("vip1");// 方式二://  auth.userDetailsService(authUserDetailsService).passwordEncoder(new BCryptPasswordEncoder());}}

方式二:连接数据库

1、Bean

public class Users {private int id;private String username;private String password;private String  role;

2、usersService

@Mapper
@Repository
public interface UsersService {Users findUser(@PathParam("username") String username);
}

3、核心

@Component
public class AuthUserDetailsService  implements UserDetailsService {@AutowiredUsersService usersService;@Overridepublic UserDetails loadUserByUsername(String username)throws UsernameNotFoundException   {Users userInfo = usersService.findUser(username);if(userInfo==null){throw  new UsernameNotFoundException("当前用户不存在");}List<GrantedAuthority> grantedAuthorityList=new ArrayList<>();grantedAuthorityList.add(new SimpleGrantedAuthority("ROLE_"+userInfo.getRole()));return new User(userInfo.getUsername(),new BCryptPasswordEncoder().encode(userInfo.getPassword()),grantedAuthorityList);}
}

4、上文SecurityConfig

Spring Security(安全)相关推荐

  1. spring boot整合spring security笔记

    最近自己做了一个小项目,正在进行springboot和spring Security的整合,有一丢丢的感悟,在这里分享一下: 首先,spring boot整合spring security最好是使用T ...

  2. Spring Security 实战干货:自定义异常处理

    Spring Security 实战干货:自定义异常处理 转自:https://www.cnblogs.com/felordcn/p/12142514.html 文章目录 1. 前言 2. Sprin ...

  3. Spring security防止跨站请求伪造(CSRF防护)

    因为使用了spring security 安全性框架 所以spring security 会自动拦截站点所有状态变化的请求(非GET,HEAD,OPTIONS和TRACE的请求),防止跨站请求伪造(C ...

  4. 【Spring Security】五、自定义过滤器

    在之前的几篇security教程中,资源和所对应的权限都是在xml中进行配置的,也就在http标签中配置intercept-url,试想要是配置的对象不多,那还好,但是平常实际开发中都往往是非常多的资 ...

  5. SpringBoot整合Spring Security

    个人资源与分享网站:http://xiaocaoshare.com/ SpringSecurity核心功能: 认证(你是谁) 授权(你能干什么) 攻击防护(防止伪造身份) 1.pom.xml < ...

  6. spring security remember me实现自动登录

    1 默认策略 在我们自定义的login中增加一个选择框 <input type="submit" value="Login" /> <br/& ...

  7. Spring security获取当前用户

    1.如果在jsp页面中获取可以使用spring security的标签 页面引入标签 [java] view plain copyprint? <%@ taglib prefix="s ...

  8. Spring Security 之集群Session配置

    1.   新建Maven项目 cluster-session 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4.0. ...

  9. Spring Security 中最流行的权限管理模型!

    前面和大家说了 ACL,讲了理论,也给了一个完整的案例,相信小伙伴们对于 ACL 权限控制模型都已经比较了解了. 本文我要和大家聊一聊另外一个非常流行的权限管理模型,那就是 RBAC. 1.RBAC ...

  10. Spring boot使用Spring Security和OAuth2保护REST接口

    在本文中,我将会演示SpringSecurity+OAuth2如何去保护SpringBoot上的RESTAPI端点,以及客户端和用户凭证如何存储在关系数据库中.因此,我们需要做到以下几点: 配置Spr ...

最新文章

  1. Android应用程序消息处理机制(Looper、Handler)分析(2)
  2. VMware8.0虚拟机中安装Ubuntu12.04使用NAT设置连接网络
  3. 员工发布“宏颜获水”广告引争议 苏宁回应:系个人行为
  4. Java开发笔记(一百二十六)Swing的窗口
  5. 阿里云云计算 23 VPC的基础架构
  6. Redis安装教程【Windows]
  7. 透视相机怎么得到正交效果
  8. Eclipse设置护眼豆沙绿
  9. Leetcode刷题95. 不同的二叉搜索树 II
  10. 列表 元组 字典的概念及其案例
  11. android 面包屑控件,Android —— 面包屑控件(BreadcrumbTreeView)
  12. 图文详解:如何给女朋友解释什么是微服务?
  13. 用大白菜装centos7_u盘安装centos 卡住大白菜怎么用u盘装win7系统
  14. 萌新带你开车上p站(二)
  15. android平板电脑怎么才能连接电脑,平板连接电脑没反应怎么办 平板怎样连接电脑...
  16. Android——一个简单的音乐APP(二)
  17. Word2010专项试题
  18. 富文本编辑器 Kindeditor 的使用和 常见错误
  19. Linux命令 nslookup,每天学一个 Linux 命令(69):nslookup
  20. SSM整合Ueditor的上传图片功能常见错误解决办法

热门文章

  1. linux内核编译时bad register name `%dil'错误
  2. 动手学深度学习:3.16 实战Kaggle比赛:房价预测
  3. session的到底是做什么的?
  4. 智能家居系统模型设计2.0
  5. 中国麻纺行业竞争动态及产销需求预测报告(2022-2027年)
  6. 【算法】剑指offer - JZ76 删除链表中重复的结点
  7. #触摸一体机##五指息屏#
  8. 达梦数据库和mysql索引引擎_达梦数据库如何建索引和使用
  9. 搭建一个完整的实时日志统计平台
  10. 知乎版ChatGPT「知海图AI」加入国产大模型乱斗,称效果与GPT-4持平