怎么让人理解网站建设,在线p图网页版免费,人人秀h5制作软件下载,.net网站开发流程1、SpringSecurity
1.1 SpringSecurity简介
Spring Security是基于Spring的安全框架,提供了包含认证和授权的落地方案#xff1b;Spring Security底层充分利用了Spring IOC和AOP功能#xff0c;为企业应用系统提供了声明式安全访问控制解决方案#xff1b;SpringSecurity可…1、SpringSecurity
1.1 SpringSecurity简介
Spring Security是基于Spring的安全框架,提供了包含认证和授权的落地方案Spring Security底层充分利用了Spring IOC和AOP功能为企业应用系统提供了声明式安全访问控制解决方案SpringSecurity可在Web请求级别和方法调用级别处理身份认证和授权为应用系统提供声明式的安全访问控制功能
官网地址: https://spring.io/projects/spring-security
1.2 SpringSecurity工程搭建
导入基础工程day10-分库分表项目集成和安全框架\资料\security资料\基础代码\security_demo
A. 创建工程引入依赖
parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.3.4.RELEASE/versionrelativePath/
/parent
dependencies!-- web起步依赖 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- springBoot整合Security --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId/dependency!-- lombok --dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency
/dependenciesB. 引导类
SpringBootApplication
public class MySecurityApplication {public static void main(String[] args) {SpringApplication.run(MySecurityApplication.class,args);}
}C. Controller
RestController
public class UserController {GetMapping(/hello)public String hello(){return hello security;}GetMapping(/say)public String say(){return say security;}GetMapping(/register)public String register(){return register security;}
}D. 测试
访问: http://localhost:8080/hello
会自动拦截并跳转到登录页面SpringSecurity提供登录之后才可以访问 而登录的用户名和密码都是SpringSecurity中内置的默认的用户名密码 用户名为user 密码为控制台输出的一段随机数 效果 登录成功之后会自动跳转到之前访问的地址 注意
# 我们也可在配置文件中配置用户名和密码实际开发中密码不应明文配置
spring.security.user.nameuser
spring.security.user.password66662、SpringSecurity自定义认证配置
在上述入门程序中, 用户名、密码都是框架默认帮我们生成的, 方式不够友好SpringSecurity也为我们提供了通过配置的形式声明合法的账户信息的方式。
A.声明配置类定义用户名密码信息
Configuration
EnableWebSecurity//开启web安全设置生效
public class SecurityConfig extends WebSecurityConfigurerAdapter {/*** 构建认证服务并将对象注入spring IOC容器用户登录时会调用该服务进行用户合法信息认证* return*/Beanpublic UserDetailsService userDetailsService(){//从内存获取用户认证信息的服务类了解后期用户的信息要从表中获取InMemoryUserDetailsManager inMemoryUserDetailsManager new InMemoryUserDetailsManager();//构建用户,真实开发中用户信息要从数据库加载构建UserDetails u1 User.withUsername(itcast).password({noop}123456)//{noop}:no operration--》表示登录时对避免不做任何操作说白了就是明文比对.authorities(P5, ROLE_ADMIN)//用户的权限信息.build();UserDetails u2 User.withUsername(itheima).password({noop}123456).authorities(P7, ROLE_SELLER,ROLE_ADMIN)//如果角色也作为一种权限资源则角色名称的前缀必须加ROLE_.build();inMemoryUserDetailsManager.createUser(u1);inMemoryUserDetailsManager.createUser(u2);return inMemoryUserDetailsManager;}
}说明 1.在userDetailsService()方法中 返回了一个UserDetailsService对象给spring容器管理当用户发生登录认证行为时Spring Security底层会自动调用UserDetailsService类型bean提供的用户信息进行合法比对如果比对成功则资源放行否则就认证失败 2.当前暂时使用InMemoryUserDetailsManager实现类后续我们也可手动实现UserDetailsService接口做最大程度的自定义 B.测试配置账户信息
通过测试配置的账户和密码信息都是有效的。
4、SpringSecurity自定义授权配置
经过上一小结配置我们发现用户认证通过后资源是都可被访问的。如果我们想为不同的用户指定不同的访问资源该如何实现呢
接下来我们通过配置为不同用户访问授权。
4.1 基于编码方式定义授权
Configuration
EnableWebSecurity
//EnableGlobalMethodSecurity(prePostEnabled true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {BeanOverridepublic UserDetailsService userDetailsService(){InMemoryUserDetailsManager inMemoryUserDetailsManager new InMemoryUserDetailsManager();inMemoryUserDetailsManager.createUser(User.withUsername(itcast).password({noop}123456).authorities(P1,ROLE_ADMIN).build());inMemoryUserDetailsManager.createUser(User.withUsername(itheima).password({noop}123456).authorities(O1,ROLE_SELLER).build());return inMemoryUserDetailsManager;}Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin()//开启默认form表单登录方式.and().logout()//登出用默认的路径登出 /logout.permitAll()//允许所有的用户访问登录或者登出的路径.and().csrf().disable()//启用CSRF,防止CSRF攻击.authorizeRequests()//授权方法该方法后有若干子方法进行不同的授权规则处理//允许所有账户都可访问不登录即可访问,同时可指定多个路径.antMatchers(/register).permitAll()//开发方式1基于配置
// .antMatchers(/a1,/a2).hasRole(seller)//拥有seller角色的用户可访问a1和a2资源//拥有指定的任意角色都可以访问对应资源
// .antMatchers(/b1).hasAnyRole(manager1,manager2)//用户任意指定的aa bb都可以访问c1资源
// .antMatchers(/c1).hasAnyAuthority(aa,bb)
// .antMatchers(/d).denyAll()//拒绝任意用户访问
// .antMatchers(/e).anonymous()//允许匿名访问//指定IP可以访问
// .antMatchers(/f).hasIpAddress(localhost/82).antMatchers(/hello).hasAuthority(P5) //具有P5权限才可以访问.antMatchers(/say).hasRole(ADMIN) //具有ROLE_ADMIN 角色才可以访问.anyRequest().authenticated(); //除了上边配置的请求资源其它资源都必须授权才能访问}
}CSRFCross-site request forgery跨站请求伪造也被称为One Click Attack或者 Session Riding通常缩写为 CSRF 或者 XSRF是一种对网站的恶意利用。 4.2 基于注解方式定义授权
基于注解的方式维护权限更偏向于集中化配置管理但是这种方式带来的问题是随着权限管理的资源的增多会导致权限配置变得十分的臃肿所以SpringSecurity为我们提供了基于注解的配置方式。
A.在配置类开启security的前置注解
//开启SpringSecurity相关注解支持
EnableGlobalMethodSecurity(prePostEnabled true)B.调整资源配置类 Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin()//定义认证时使用form表单的方式提交数据.and().logout()//登出用默认的路径登出 /logout.permitAll()//允许所有的用户访问登录或者登出的路径,如果 .anyRequest().authenticated()注释掉则必须添加permitAll()否则就不能正常访问登录或者登出的路径.and().csrf().disable().authorizeRequests()//授权方法该方法后有若干子方法进行不同的授权规则处理//允许所有账户都可访问不登录即可访问,同时可指定多个路径
// .antMatchers(/register).permitAll()//开发方式1基于配置
// .antMatchers(/a1,/a2).hasRole(seller)//拥有seller角色的用户可访问a1和a2资源//拥有指定的任意角色都可以访问对应资源
// .antMatchers(/b1).hasAnyRole(manager1,manager2)//用户任意指定的aa bb都可以访问c1资源
// .antMatchers(/c1).hasAnyAuthority(aa,bb)
// .antMatchers(/d).denyAll()//拒绝任意用户访问
// .antMatchers(/e).anonymous()//允许匿名访问//指定IP可以访问
// .antMatchers(/f).hasIpAddress(localhost/82)
// .antMatchers(/hello).hasAuthority(P5) //具有P5权限才可以访问
// .antMatchers(/say).hasRole(ADMIN) //具有ROLE_ADMIN 角色才可以访问.anyRequest().authenticated(); //与注解PermitAll冲突需要注掉该行}C.注解配置资源权限
在控制方法/URL的权限时, 可以通过配置类中配置的方式进行控制, 也可以使用 注解 PreAuthorize 来进行控制, 推荐使用注解: /*** PreAuthorize:指在注解作用的方法执行之前做权限校验* PostAuthorize:指在注解作用的方法执行之后做权限校验* return*/PreAuthorize(hasAuthority(P5))
// PostAuthorize(hasAuthority(P4))GetMapping(/hello)public String hello(){return hello security;}PreAuthorize(hasRole(ADMIN))GetMapping(/say)public String say(){return say security;}PermitAll//等价于antMatchers(/register).permitAll()//任何用户都可访问//PreAuthorize(isAnonymous())GetMapping(/register)public String register(){return register security;}说明使用PreAuthorize,需要开启全局方法授权开关,加上注解EnableGlobalMethodSecurity(prePostEnabledtrue)