Spring Security权限框架 | 您所在的位置:网站首页 › java权限认证框架 › Spring Security权限框架 |
目录
为什么需要权限管理? 权限管理的核心是什么? 权限框架主要有? Spring Security Spring Security实战 1.搭建springbooot环境 2.只要能登录即可的例子 2.基于内存的权限设置 3.角色进行拦截
为什么需要权限管理? 安全性:误操作,人为破坏,数据泄露等数据隔离:不同权限能看到及操作不同的数据明确职责:运营,客服等不同角色,等级不同 权限管理的核心是什么? 1.用户-权限:人员少,功能固定,或特别简单的系统 2.RBAC:用户-角色-权限 所有系统都适用 1. Spring Security 2.apache shiro 本博客主要讲Spring Security Spring Security
主要是认证和验证 Basic 认证:Basic 认证是HTTP 中非常简单的认证方式,因为简单,所以不是很安全,不过仍然非常常用。 Digest 认证:客户端请求资源->服务器返回认证标示->客户端发送认证信息->服务器查验认证,如果成功则继续资源传送,否则直接断开连接。 x.509认证:数字认证 LDAP 认证:LDAP认证是通过WSS3.0加上轻量目录LDAP协议搭建的种认证方式,使用https加密传输,主要用于做文档管理。 From 认证:表单认证
下面使用springboot+Spring Security 实战下 Spring Security实战 1.搭建springbooot环境https://start.spring.io/ 搭建springboot网站,选择如下两个依赖,我这里使用1.5.6.RELEASE的springboot
搭建好了之后下载下来解压,导入IDEA 中 运行项目,能成功启动即可,如下不报错,就说明这个工程没有问题。
在主程序DemoApplication 中编写两个方法,一个是用来访问主页用( home()),一个是需要登录才能访问(hello()) package com.su.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController @EnableAutoConfiguration public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @RequestMapping("/") public String home() { return "您好,主页"; } @RequestMapping("/hello") public String hello() { return "hello路径"; } }
新建一个方法SpringSecurityConfig ,主要是用户进行权限设置的
package com.su.demo; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; /** *主要是权限配置 */ @Configuration @EnableWebSecurity public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { /** * 拦截下面的东西 * * @param http * @throws Exception */ @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/").permitAll() //主路径允许访问 .anyRequest().authenticated() //验证 .and() .logout().permitAll() //注销也是运行访问 .and() .formLogin(); http.csrf().disable(); //关闭csrf() 认证 } /** * 不拦截下面这些资源 * * @param web * @throws Exception */ @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/js/**", "/css/**", "/images/**"); } } 运行程序,效果:运行/ 允许访问 ,运行/hello 是需要被拦截的 2.基于内存的权限设置 在方法SpringSecurityConfig 中添加代码 /** * 基于内存的验证:不需要用到数据库的情况 * * @param auth * @throws Exception */ @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //我们指定一个人这个人的用户:admin 密码:123456 角色:ADMIN auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("ADMIN"); }当访问http://localhost:8080/hello 我们需要输入存在内存中的用户名密码 3.角色进行拦截 在方法DemoApplication 中添加代码 @EnableGlobalMethodSecurity(prePostEnabled = true)//只有ADMIN 的角色才能访问/** * 只有ADMIN 的角色才能访问 * @return */ @PreAuthorize("hasRole('ROLE_ADMIN')")//必须用ROLE_xxx 才能识别你是做角色权限 @RequestMapping("/roleAuth") public String role () { return "需要某个权限才能访问"; } 运行,当访问http://localhost:8080/roleAuth 时只有角色为ADMIN 的用户才能访问
|
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |