温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot中如何对Shiro进行整合

发布时间:2021-06-17 11:58:13 来源:亿速云 阅读:126 作者:Leah 栏目:编程语言

SpringBoot中如何对Shiro进行整合,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

原生的整合

创建项目

创建一个 Spring Boot 项目,只需要添加 Web 依赖即可:

SpringBoot中如何对Shiro进行整合

项目创建成功后,加入 Shiro 相关的依赖,完整的 pom.xml 文件中的依赖如下:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-web</artifactId>
    <version>1.4.0</version>
  </dependency>
  <dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.4.0</version>
  </dependency>
</dependencies>

创建 Realm

接下来我们来自定义核心组件 Realm:

public class MyRealm extends AuthorizingRealm {
  @Override
  protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    return null;
  }
  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    String username = (String) token.getPrincipal();
    if (!"javaboy".equals(username)) {
      throw new UnknownAccountException("账户不存在!");
    }
    return new SimpleAuthenticationInfo(username, "123", getName());
  }
}

在 Realm 中实现简单的认证操作即可,不做授权,授权的具体写法和 SSM 中的 Shiro 一样,不赘述。这里的认证表示用户名必须是 javaboy ,用户密码必须是 123 ,满足这样的条件,就能登录成功!

配置 Shiro

接下来进行 Shiro 的配置:

@Configuration
public class ShiroConfig {
  @Bean
  MyRealm myRealm() {
    return new MyRealm();
  }
  
  @Bean
  SecurityManager securityManager() {
    DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
    manager.setRealm(myRealm());
    return manager;
  }
  
  @Bean
  ShiroFilterFactoryBean shiroFilterFactoryBean() {
    ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
    bean.setSecurityManager(securityManager());
    bean.setLoginUrl("/login");
    bean.setSuccessUrl("/index");
    bean.setUnauthorizedUrl("/unauthorizedurl");
    Map<String, String> map = new LinkedHashMap<>();
    map.put("/doLogin", "anon");
    map.put("/**", "authc");
    bean.setFilterChainDefinitionMap(map);
    return bean;
  }
}

在这里进行 Shiro 的配置主要配置 3 个 Bean :

  • 首先需要提供一个 Realm 的实例。

  • 需要配置一个 SecurityManager,在 SecurityManager 中配置 Realm。

  • 配置一个 ShiroFilterFactoryBean ,在 ShiroFilterFactoryBean 中指定路径拦截规则等。

  • 配置登录和测试接口。

其中,ShiroFilterFactoryBean 的配置稍微多一些,配置含义如下:

  • setSecurityManager 表示指定 SecurityManager。

  • setLoginUrl 表示指定登录页面。

  • setSuccessUrl 表示指定登录成功页面。

  • 接下来的 Map 中配置了路径拦截规则,注意,要有序。

这些东西都配置完成后,接下来配置登录 Controller:

@RestController
public class LoginController {
  @PostMapping("/doLogin")
  public void doLogin(String username, String password) {
    Subject subject = SecurityUtils.getSubject();
    try {
      subject.login(new UsernamePasswordToken(username, password));
      System.out.println("登录成功!");
    } catch (AuthenticationException e) {
      e.printStackTrace();
      System.out.println("登录失败!");
    }
  }
  @GetMapping("/hello")
  public String hello() {
    return "hello";
  }
  @GetMapping("/login")
  public String login() {
    return "please login!";
  }
}

测试时,首先访问 /hello 接口,由于未登录,所以会自动跳转到 /login 接口:

SpringBoot中如何对Shiro进行整合

然后调用 /doLogin 接口完成登录:

SpringBoot中如何对Shiro进行整合

再次访问 /hello 接口,就可以成功访问了:

SpringBoot中如何对Shiro进行整合

使用 Shiro Starter

上面这种配置方式实际上相当于把 SSM 中的 XML 配置拿到 Spring Boot 中用 Java 代码重新写了一遍,除了这种方式之外,我们也可以直接使用 Shiro 官方提供的 Starter 。

创建工程,和上面的一样

创建成功后,添加 shiro-spring-boot-web-starter ,这个依赖可以代替之前的 shiro-webshiro-spring 两个依赖,pom.xml 文件如下:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring-boot-web-starter</artifactId>
    <version>1.4.0</version>
  </dependency>
</dependencies>

创建 Realm

这里的 Realm 和前面的一样,我就不再赘述。

配置 Shiro 基本信息

接下来在 application.properties 中配置 Shiro 的基本信息:

shiro.sessionManager.sessionIdCookieEnabled=true
shiro.sessionManager.sessionIdUrlRewritingEnabled=true
shiro.unauthorizedUrl=/unauthorizedurl
shiro.web.enabled=true
shiro.successUrl=/index
shiro.loginUrl=/login

配置解释:

  • 第一行表示是否允许将sessionId 放到 cookie 中

  • 第二行表示是否允许将 sessionId 放到 Url 地址拦中

  • 第三行表示访问未获授权的页面时,默认的跳转路径

  • 第四行表示开启 shiro

  • 第五行表示登录成功的跳转页面

  • 第六行表示登录页面

配置 ShiroConfig

@Configuration
public class ShiroConfig {
  @Bean
  MyRealm myRealm() {
    return new MyRealm();
  }
  @Bean
  DefaultWebSecurityManager securityManager() {
    DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
    manager.setRealm(myRealm());
    return manager;
  }
  @Bean
  ShiroFilterChainDefinition shiroFilterChainDefinition() {
    DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
    definition.addPathDefinition("/doLogin", "anon");
    definition.addPathDefinition("/**", "authc");
    return definition;
  }
}

关于SpringBoot中如何对Shiro进行整合问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI