java web安全框架_7.1 SpringSecurity安全框架

一.SpringSecurity概述

1.什么是安全

在web开发中,安全第一位!之前使用的过滤器和拦截器都是为了安全的目的。

不是网站的功能性需求

应该在网站设计之初就考虑安全,防止隐私泄露等安全问题

shiro、SpringSecurity框架:除了类不同和名字不一样,基本很像都是认证和授权等。

2.Spring Security简介

(1)SpringSecurity是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理。

(2)几个重要的类

WebSecurityConfigurerAdapter:自定义Security策略

AuthenticationManagerBuilder:自定义认证策略

@EnableWebSecurity:开启WebSecurity模式,@Enablexxxx开启某个功能

(3)SpringSecurity的两个主要目标是“认证”和“授权”(访问控制)

“认证”(Authentication)

“授权”(Authorization)

这个概念是通用的,而不是只在SpringSecurity中存在

(4)springsecurity网址

官网:https://spring.io/projects/spring-security

JavaConfig配置:https://docs.spring.io/spring-security/site/docs/5.2.2.RELEASE/reference/htmlsingle/#jc

3.使用Spring Security

(0)步骤:项目根据用户权限显示不同权限用户所能见的内容

新建springboot项目,添加springmvc,springsecurity,thymeleaf等模块(略)

pom.xml导入依赖thymeleaf-extras-springsecurity5

导入静态资源和页面并配置管理thymeleaf缓存

编写路由控制器

以Java Config方式编写配置文件

前端页面使用thymeleaf-extras-springsecurity5

测试

(1)pom.xml导入依赖thymeleaf-extras-springsecurity5

thymeleaf-extras-springsecurity5是thymeleaf和springsecurity所整合的包

org.thymeleaf.extras

thymeleaf-extras-springsecurity5

3.0.4.RELEASE

(2)导入静态资源和页面并配置管理thymeleaf缓存

8da23989fe1103ce4ad75b5651591ff4.png

spring.thymeleaf.cache=false

(3)编写路由控制器

建立controller文件夹并在其下建立RouterController.java

1 importorg.springframework.stereotype.Controller;2 importorg.springframework.web.bind.annotation.PathVariable;3 importorg.springframework.web.bind.annotation.RequestMapping;4

5 @Controller6 public classRouterController {7

8 @RequestMapping({"/", "/index"})9 publicString index() {10 return "index";11 }12

13 @RequestMapping("/toLogin")14 publicString toLogin() {15 return "views/login";16 }17

18 @RequestMapping("/level1/{id}")19 public String level1(@PathVariable("id") intid) {20 return "views/level1/" +id;21 }22

23 @RequestMapping("/level2/{id}")24 public String level2(@PathVariable("id") intid) {25 return "views/level2/" +id;26 }27

28 @RequestMapping("/level3/{id}")29 public String level3(@PathVariable("id") intid) {30 return "views/level3/" +id;31 }32 }

(4)以Java Config方式编写配置文件

在controller同一级添加config文件夹并在其下建立SecurityConfig.java文件

重写授权方法:

此处设置了四个授权:首页所有人可访问,level1目录下的页面等级vip1可访问,level2目录下的页面等级vip2可访问,level3目录下的页面等级vip3可访问

如果没有权限访问默认跳转到springsecurity的登陆页面

定义登出页面为首页

关闭跨站脚本攻击

重写认证方法

设置了三个用户以及这三个用户的权限

1 importorg.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;2 importorg.springframework.security.config.annotation.web.builders.HttpSecurity;3 importorg.springframework.security.config.annotation.web.configuration.EnableWebSecurity;4 importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;5 importorg.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;6

7 //AOP

8 @EnableWebSecurity9 public class SecurityConfig extendsWebSecurityConfigurerAdapter {10

11 //授权

12 @Override13 protected void configure(HttpSecurity http) throwsException {14

15

16 //首页所有人可以访问,功能页只有对应有权限的人才可以访问17 //请求授权的规则,链式编程

18 http.authorizeRequests()19 .antMatchers("/").permitAll()20 .antMatchers("/level1/**").hasRole("vip1")21 .antMatchers("/level2/**").hasRole("vip2")22 .antMatchers("/level3/**").hasRole("vip3");23

24 //没有权限默认会到登录页面,需要开启登录的页面login

25 http.formLogin();26

27 //注销,开启注销功能,注销后会跳转到/logout28 //可以清空Cookie和Session29 //http.logout().deleteCookies("remove").invalidateHttpSession(true);

30 http.logout().logoutSuccessUrl("/");31

32 //防止跨站脚本攻击关闭

33 http.csrf().disable();34 }35

36 //认证,在Spring Security 5.0+中需要对密码加密passwordEncoder

37 @Override38 protected void configure(AuthenticationManagerBuilder auth) throwsException {39

40 //这些数据正常从数据库中读,此处使用inMemoryAuthentication从内存中读

41 auth.inMemoryAuthentication().passwordEncoder(newBCryptPasswordEncoder())42 .withUser("wzh").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")43 .and()44 .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")45 .and()46 .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");47 }48 }

(5)前端页面使用thymeleaf-extras-springsecurity5

定义命名空间: xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"

判断是否登录: sec:authorize="!isAuthenticated()"

获得登录用户名: sec:authentication="name"

获得登录用户权限: sec:authentication="principal.authorities"

判断是否有xxx权限: sec:authorize="hasRole('vip1')"

首页

首页

登录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值