springboot+shiro/SpringSecurity登陆验证

Shiro

1. Shiro三个核心组件

1.1 Subject
  Subject:即“当前操作用发户”。但是,在Shiro中,Subject这一概念并不仅仅指人,也可以是第三方进程、后台帐户(Daemon Account)或其他类似事物。它仅仅意味着“当前跟软件交互的东西”。但考虑到大多数目的和用途,你可以把它认为是Shiro的“用户”概念。Subject代表了当前用户的安全操作,SecurityManager则管理所有用户的安全操作。

1.2 SecurityManager
  SecurityManager:它是Shiro框架的核心,典型的Facade模式,Shiro通过SecurityManager来管理内部组件实例,并通过它来提供安全管理的各种服务

1.3 Realm
  Realm充当了Shiro与应用安全数据间的“桥梁”或者“连接器”。也就是说,当对用户执行认证(登录)和授权(访问控制)验证时,Shiro会从应用配置的Realm中查找用户及其权限信息。
  
  从这个意义上讲,Realm实质上是一个安全相关的DAO:它封装了数据源的连接细节,并在需要时将相关数据提供给Shiro。当配置Shiro时,你必须至少指定一个Realm,用于认证和(或)授权。配置多个Realm是可以的,但是至少需要一个。
  Shiro内置了可以连接大量安全数据源(又名目录)的Realm,如LDAP、关系数据库(JDBC)、类似INI的文本配置资源以及属性文件等。如果缺省的Realm不能满足需求,你还可以插入代表自定义数据源的自己的Realm实现。

2. Shiro相关类介绍

(1)Authentication 认证 ---- 用户登录
(2)Authorization 授权 — 用户具有哪些权限
(3)Cryptography 安全数据加密
(4)Session Management 会话管理
(5)Web Integration web系统集成
(6)Interations 集成其它应用,spring、缓存框架

3. Shiro 特点

(1)易于理解的 Java Security API;
(2)简单的身份认证(登录),支持多种数据源(LDAP,JDBC,Kerberos,ActiveDirectory 等);
(3)对角色的简单的签权(访问控制),支持细粒度的签权;
(4)支持一级缓存,以提升应用程序的性能;
(5)内置的基于 POJO 企业会话管理,适用于 Web 以及非 Web 的环境;
(6)异构客户端会话访问;
(7)非常简单的加密 API;
(8)不跟任何的框架或者容器捆绑,可以独立运行

4. 使用Springboot+shiro+jwt实现登陆验证

以下博文超级详细,我不再赘述
https://www.jianshu.com/p/0b1131be7ace
源码地址:https://github.com/chilexun/springboot-demo
其中包含Springboot+shiro和Springboot+Spring Security实现的代码,拿来即用,使用方法见以上博文

5. Springboot+shiro+redis实现登陆验证例子(适合中小行项目)

基本思想:登录时候生成token<K,V>,将K返回给前端,前端每次请求的时候将K放在header中,并同时将K,V信息存储到redis中,用于拦截每次登录的验证。
手机号码登录时:
在这里插入图片描述
请求权限验证时使用shiro进行拦截和验证:
在这里插入图片描述

Spring Security

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

记住几个类:

  • WebSecurityConfigurerAdapter:自定义Security策略

  • AuthenticationManagerBuilder:自定义认证策略

  • @EnableWebSecurity:开启WebSecurity模式

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

“认证”(Authentication)

身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。

身份验证通常通过用户名和密码完成,有时与身份验证因素结合使用。

“授权” (Authorization)

授权发生在系统成功验证您的身份后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。

这个概念是通用的,而不是只在Spring Security 中存在。
具体使用方法见上面的提到的代码:
以下博文超级详细,我不再赘述
https://www.jianshu.com/p/0b1131be7ace
源码地址:https://github.com/chilexun/springboot-demo

SpringSecurity配置的例子

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity//开启SpringSecurity验证
public class SecurityConfig extends WebSecurityConfigurerAdapter {

   //定制请求的授权规则
   @Override
   protected void configure(HttpSecurity http) throws Exception {
       http.authorizeRequests().antMatchers("/").permitAll()
      .antMatchers("/level1/**").hasRole("vip1")//只有vip1权限的用户能访问/level1/前缀的url
      .antMatchers("/level2/**").hasRole("vip2")//只有vip2权限的用户能访问/level2/前缀的url
      .antMatchers("/level3/**").hasRole("vip3");//只有vip3权限的用户能访问/level3/前缀的url
      
       //开启自动配置的登录功能:如果没有权限,就会跳转到登录页面!
       // /login 请求来到登录页
       // /login?error 重定向到这里表示登录失败
       http.formLogin()
          .usernameParameter("username")
          .passwordParameter("password")
          .loginPage("/toLogin")
          .loginProcessingUrl("/login"); // 登陆表单提交请求

       //开启自动配置的注销的功能
           // /logout 注销请求
           // .logoutSuccessUrl("/"); 注销成功来到首页

       http.csrf().disable();//关闭csrf功能:跨站请求伪造,默认只能通过post方式提交logout请求
       http.logout().logoutSuccessUrl("/");

       //记住我
       http.rememberMe().rememberMeParameter("remember");
  }

   //定义认证规则
   @Override
   protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       //在内存中定义,也可以在jdbc中去拿....
       //Spring security 5.0中新增了多种加密方式,也改变了密码的格式。
       //要想我们的项目还能够正常登陆,需要修改一下configure中的代码。我们要将前端传过来的密码进行某种方式加密
       //spring security 官方推荐的是使用bcrypt加密方式。

       auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
              .withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
              .and()
              .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
              .and()
              .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2");
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring BootShiro的结合可以实现灵活可扩展的权限管理系统。以下是一些可能的设计和实现方案: 1. 集成Shiro 首先,需要在pom.xml文件中添加Shiro的依赖: ``` <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.2</version> </dependency> ``` 然后,需要在Spring Boot应用程序的配置文件中添加Shiro配置信息: ``` shiro: filterChainDefinitions: /login = anon /logout = logout /** = authc securityManager: loginUrl: /login successUrl: /home unauthorizedUrl: /unauthorized realms: - name: myRealm authenticationCacheName: authenticationCache authorizationCacheName: authorizationCache ``` 在这个配置中,filterChainDefinitions指定了URL的访问规则,securityManager指定了登录、成功和未授权的URL,realms指定了数据源。 2. 定义Realm 定义Realm来实现Shiro的认证和授权功能。Realm是Shiro的核心组件之一,它是一个安全数据源,用于验证用户的身份和授权用户的访问权限。可以通过实现Realm接口来定义自己的Realm,也可以使用Shiro提供的现有的Realm实现。 自定义Realm需要实现doGetAuthenticationInfo和doGetAuthorizationInfo两个方法,分别用于身份验证和授权。 3. 集成Spring Security 另一种方案是使用Spring Security来实现权限管理。Spring SecuritySpring框架中的一个安全框架,它提供了身份验证、授权和其他安全功能。 集成Spring Security需要在pom.xml文件中添加Spring Security的依赖: ``` <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>5.5.0</version> </dependency> ``` 然后,需要在应用程序的配置文件中添加Spring Security配置信息: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private DataSource dataSource; @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasRole("USER") .antMatchers("/login/**").permitAll() .and().formLogin(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.jdbcAuthentication().dataSource(dataSource) .usersByUsernameQuery("select username, password, enabled from users where username=?") .authoritiesByUsernameQuery("select username, authority from authorities where username=?"); } } ``` 在这个配置中,configure方法用于配置HttpSecurity,authorizeRequests方法用于定义URL的访问规则,formLogin方法用于指定登录页面。configure方法用于配置AuthenticationManagerBuilder,jdbcAuthentication方法用于指定数据源和查询用户信息和角色信息的SQL语句。 总之,Spring BootShiroSpring Security的集成可以实现灵活可扩展的权限管理系统,可以根据具体业务需求选择适合的方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值