Shiro安全框架(2)

简介: Shiro安全框架

2.第一个文件为UserRealm文件:

package com.whx.config;
import com.whx.pojo.User;
import com.whx.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
public class UserRealm  extends AuthorizingRealm {
    @Autowired
    UserService userService;
//    授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了>=doGetAuthorizationInfo()");
        return null;
    }
//    认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行了>=doGetAuthenticationInfo()");
        用户名和密码从数据库中取,此处先伪造一些数据方便验证
//        String name="root";
//        String password="123456";
//        UsernamePasswordToken userToken=(UsernamePasswordToken)authenticationToken;
用户名验证
//        if(!userToken.getUsername().equals(name)){
//            return null;
//        }
//        通过数据库连接
        UsernamePasswordToken userToken=(UsernamePasswordToken)authenticationToken;
        User user = userService.queryUserByName(userToken.getUsername());
        if(user==null){
            return null;
        }
//        密码验证,有shiro自己做,可以加密MD5以及MD5延值加密
        return new SimpleAuthenticationInfo("",user.getPwd(),"");
    }
}

用假数据写用户名和密码时,可以通过以下进行测试是否书写正确。

package com.whx;
import com.whx.service.UserServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootShiro1ApplicationTests {
    @Autowired
    UserServiceImpl userService;
    @Test
    void contextLoads() {
        System.out.println(userService.queryUserByName("小儿"));
    }
}

在shiro配置文件中设置了权限,怎么赋值给用户呐?

0cadfa18f08245b9a040b2eaef708301.png

死数据取值:

5f387e43d93846c9acb7f69222075788.png


通过从数据库取值,在数据库中添加一条perms属性:

453b0ccd655c48c9979cb07900e58df6.png84e42214b0294f899e2a6349dfd6ecc1.png

1.shiro与thymeleaf的整合

1.1 首先导入依赖

  <dependency>
      <groupId>com.github.theborakompanioni</groupId>
      <artifactId>thymeleaf-extras-shiro</artifactId>
      <version>2.0.0</version>
  </dependency>


1.2 完成使用前的配置

7e9623b5d4354e2b8ca4f22082f8ee55.png

在ShiroConfig文件中进行配置:

@Bean
    public ShiroDialect getShiroDialect(){
       return new ShiroDialect();


1.3相关的应用实例

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<div >
    <a th:href="@{/toLogin}">登录</a>
<div/>
<p th:text="${msg}"></p>
<hr>
<div shiro:hasPermission="user:add">
    <a th:href="@{/user/add}">add</a>
</div>
<div shiro:hasPermission="user:update">
    <a th:href="@{/user/update}">update</a>
</div>
</body>
</html>
相关文章
|
存储 缓存 安全
【安全框架】快速了解安全框架
安全框架简单来说就是对访问权限进行控制,主要是用户认证和权限鉴权。介绍市面常见的安全框架。
313 0
【安全框架】快速了解安全框架
|
7月前
|
存储 缓存 安全
02 Shiro的架构
02 Shiro的架构
29 0
|
9月前
|
缓存 安全 算法
Shiro安全框架面试题
Shiro安全框架面试题
89 0
|
10月前
|
SQL 存储 缓存
Shiro安全框架简介
基本上只要涉及到用户参数的系统都要进行权限管理,使用权限管理实现了对用户访问系统的控制,不同的用户访问不同的资源。按照安全规则或者安全策略控制用户访问资源,而且只能访问被授权的资源 权限管理包括认证和授权两部分,当用户访问资源时先对其进行身份的认证,认证通过后即可访问已经授权的资源。
52 0
|
12月前
|
存储 缓存 安全
Shiro和Spring Security安全框架对比
Shiro和Spring Security安全框架对比
452 0
|
存储 缓存 安全
Shiro框架01之什么是shiro+shiro的架构+权限认证
Shiro框架01之什么是shiro+shiro的架构+权限认证
Shiro框架01之什么是shiro+shiro的架构+权限认证
|
SQL 安全 Java
Shiro - 基础篇(下)
Shiro - 基础篇(下)
97 0
Shiro - 基础篇(下)
|
缓存 Java 数据库
Shiro - 基础篇(上)
Shiro - 基础篇(上)
119 0
Shiro - 基础篇(上)
|
缓存 安全 Java
Shiro(一):shiro简介
Shiro(一):shiro简介
180 0
Shiro(一):shiro简介