• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot整合Shiro的步骤

springboot 搞代码 4年前 (2022-01-09) 29次浏览 已收录 0个评论

1.创建一个springboot项目

选中web和thymeleaf

1.1新建index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>首页</h1>
    <p th:text="${msg}"></p>
</body>
</html>

1.2创建一个controller

package com.yao.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class MyController {
 
    @RequestMapping({"/","/index"})
    public String toIndex(Model model){
        model.addAttribute("msg","hello,Shiro");
        return "index";
    }
}

一定要记住shiro的三大对象

1.subject:本文来源gaodaimacom搞#^代%!码&网(用户

2.SecurityManager:管理所有用户

3.Realm:连接数据

1.3导入整合用的依赖包

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.4.1</version>
</dependency>

1.4创建一个config(ShiroConfig),并编写他

package com.yao.config;
 
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class ShiroConfig {
 
    //ShiroFilterFactoryBean
 
    //DefaultWebSecurityManager
 
    //创建 realm 对象,这个realm对象需要自定义
     
}

1.5创建自己的一个realmconfig,也就是在config中创建另外一个配置类UserRealm

package com.yao.config;
 
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
 
//自定义的 UserRealm
public class UserRealm extends AuthorizingRealm {
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权。。。");
        return null;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证。。。");
        return null;
    }
}

1.6将UserRealm注册到ShiroConfig里面去,是我们自己写的这个类被spring托管

1.7新建两个测试页面并重新写一下index页面


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot整合Shiro的步骤
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址