一.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缓存

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')"

首页

首页

登录

java web安全框架_7.1 SpringSecurity安全框架相关推荐

  1. Java Web——基于Servlet、JSP(无框架版)电影网站项目总结 (二,完结版)

    今天来谈谈项目的"基石"--数据库,在这个项目中,我设计了7张表,如下图所示: allmovies:存放所有电影的相关信息 clicknumber:记录每一部电影的被点击次数, c ...

  2. Java web框架

    Java web框架综述 Java Web设计框架:Web应用的Java框架综述 特金德·辛格博士,助理教授, 旁遮普巴廷达Baba Farid学院计算机科学系(印度) 摘要:在本文中,我将Web设计 ...

  3. 知乎问题:北京,2017,多少k的java web程序员应该懂多线程和jvm优化?

    知乎问题:https://www.zhihu.com/question/59725713/answer/168294369 谢邀,看你问的诚恳,我也好好回答一番. 先说一下我心目的互联网程序员分级: ...

  4. Java Web开发技术方案

    Java Web开发技术方案 Java Web开发分前端.后端: Java Web前端: -就是在Web应用中用户可以看得见碰得着的东西.包括Web页面的结构.Web的外观视觉表现以及Web层面的交互 ...

  5. Java web集成支付宝电脑支付接口(沙箱环境)

    前言 这篇博客主要介绍如何在Java web项目中集成支付宝的电脑支付接口(会稍微介绍一下服务器集成APP支付接口).目前支付宝接口更新很快,在博主查找资料的时候,很多都是即时到账接口,APP支付则是 ...

  6. 初学 Java Web 开发,请远离各种框架,从 Servlet 开发

    写在前面: 本文是转自:http://www.oschina.net/question/12_52027  的文章,如果要求删除,第一时间联系我立即删除! Web框架是开发者在使用某种语言编写Web应 ...

  7. Java框架JSON-RPC项目demo代码实战 + JAVA WEB配置虚拟目录(转自21天java web开发)

    Java框架JSON-RPC项目demo代码实战 备注  JAVA WEB配置虚拟目录(转自21天java web开发) https://blog.csdn.net/wjxbj/article/det ...

  8. 基于Stripes框架进行Java Web开发

    Mark Eagle是美国乔治亚州亚特兰大市MATRIX Resources有限公司的一位资深软件工程师,拥有Sun公司的SCP和SCWCD认证.Mark本人非常喜欢使用开源软件进行软件开发,并且多次 ...

  9. 【Java Web前端开发】前端框架 bootstrap+jquery+angularjs探索

    文章目录 1 Bootstrap 2 jQuery 3 AngularJS 1 Bootstrap 在Bootstrap的官网介绍中,Bootstrap is the most popular HTM ...

  10. 【Java Web后台实验与开发】关于SSH框架的探索

    文章目录 1 什么是SSH? 2 学习SSH方法 ? 3 MVC是什么? 3.1 MVC 编程模式 4 struts五大组件介绍 5 Spring特征 6 Hibernate特征 7 框架和设计模式的 ...

最新文章

  1. 基于近距离的测距感知传感器调研以及扩展介绍
  2. grep如何忽略.svn目录,以及如何忽略多个目录
  3. 【Android 应用开发】Android 图表绘制 achartengine 示例解析
  4. linux 关闭开机 ftp,解决linux ftp匿名上传、下载开机自启问题
  5. Java-二分查找算法
  6. Extjs4 MVC 添加view层
  7. Java EE 8发生了什么?
  8. 前端入行两年--教会了我这些道理
  9. 【代码收集】提前载入贴图
  10. input框placeholder样式修改
  11. 通过结构体某个成员的地址计算结构体首地址
  12. 化解恶劣情绪山人自有妙计
  13. 同一页面中加载两个相同的控件,结果只能出来一个,这是为什么
  14. 共阳极数码管显示c语言,基于74HC595单片机驱动数码管设计
  15. openproj jvm erron 193
  16. 国家科技管理信息系统构建及其对科技情报工作的影响
  17. Labwindows App界面文件拖拽读取
  18. Python核心编程 课后习题 第一部分
  19. vim简单用法-配合pycharm
  20. Java学习笔记 (码龄七年第一次写笔记 续2)

热门文章

  1. matlab标记最大的连通区域,Matlab得到二值图像中最大连通区域
  2. CAD二开之打开时插件命令自动加载(RibbonUI自动显示)
  3. 网站设计65条原则 作者:小柯
  4. python Beautiful Soup解析html页面table标签
  5. NNDL 作业7:第五章课后题(1×1 卷积核 | CNN BP)
  6. 面对Google流量红利期,独立站卖家如何借势营销?
  7. html获取当前ip地址_IP地址精准查询
  8. 【Spring Securtiy】A granted authority textual representation is required
  9. 目前最为出色的Wii模拟器,可以在电脑上运行绝大多数Wii游戏,对低端配置完美支持,绝对的神器
  10. android 手机存储位置设置,如何将红米手机外置SD卡设定为默认存储