shiro:整合springboot快速上手(附带代码示例)

SpringBoot整合Shiro Shiro是一个开源的Java安全框架,它提供了身份认证、授权、加密和会话管理等功能,可以帮助我们快速地构建安全可靠的应用程序。 阅读详情

shiro(安全)

学习视频链接:小狂神Springboot

每日格言

鸟欲高飞先振翅,人求上进先读书——李苦禅

shiro

阿帕奇的安全框架

Apache Shiro是一个java的安全管理框架,可以用在javaEE环境下,也可以用在javaSE环境下。

此前我们学习了很多有关阿帕奇的东西:maven,tomcat,等等

官方号称十分钟就可以入门,

官网:https://shiro.apache.org/

为什么学他?:

(1)spring security 功能完善,学习成本偏高;
(2)shiro 学习成本低,简单的安全框架,基本功能存在(登录认证,权限认证); 
(3)spring mvc interceptor(拦截器) 只能做登录认证,不能做权限认证。

他能做什么?

Authentication:身份认证/登录;

Authorization:授权;

Session Manager:会话管理;

Cryptography:加密;

Web Support:**Web支持,可以非常容易的集成到Web环境;

Caching:缓存;

Concurrency:shiro支持多线程应用的并发验证,即如在一个线程中开启另一个线程,能把权限自动传播过去;

Testing:提供测试支持;

Run As:允许一个用户假装为另一个用户(如果他们允许)的身份进行访问;

Remember Me:记住我。

Shiro不会去维护用户、维护权限;这些需要我们自己去设计/提供;然后通过相应的接口注入给Shiro。

shiro架构

Subject:主体;

SecurityManager:相当于SpringMVC中的DispatcherServlet或者Struts2中的FilterDispatcher;它管理着所有Subject、且负责进行认证和授权、及会话、缓存的管理。

Authenticator:认证器;

Authrizer:授权器,;

Realm:可以有1个或多个Realm,是安全实体数据源;可以是JDBC实现,也可以是LDAP实现,或者内存实现等等;

SessionManager:**Shiro自己的Session来管理主体与应用之间交互的数据;

SessionDAO:**DAO大家都用过,数据访问对象,用于会话的CRUD;同时SessionDao也可以使用Cache进行缓存以提高性能。

CacheManager:缓存控制器,管理如用户、角色、权限等的缓存

Cryptography:密码模块

helloshiro

导入相关依赖:官方依赖

    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-core</artifactId>
        <version>1.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.21</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.21</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

 <resources>
                <resource>
                    <directory>${basedir}/src/main/webapp</directory>
                </resource>
                <resource>
                    <directory>${basedir}/src/main/resources</directory>
                </resource>
                <resource>
                    <directory>${basedir}/src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.properties</include>
                        <include>**/*.yml</include>
                    </includes>
                </resource>
            </resources>

修改后的Quickstart类,

最新版本建议将:

FactorySecurityManager factory = new IniSecurityManagerFactory(classpath:shiro.ini);
SecurityManager securityManager = factory.getInstance();

转换位下面这样

DefaultSecurityManager securityManager = new DefaultSecurityManager();
IniRealm iniRealm = new IniRealm(classpath:shiro.ini);
securityManager.setRealm(iniRealm);

下面代码是Quickstart类:

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Simple Quickstart application showing how to use Shiro's API.
 *
 * @since 0.9 RC2
 */
public class Quickstart {

    private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);


    public static void main(String[] args) {

        // The easiest way to create a Shiro SecurityManager with configured
        // realms, users, roles and permissions is to use the simple INI config.
        // We'll do that by using a factory that can ingest a .ini file and
        // return a SecurityManager instance:

        // Use the shiro.ini file at the root of the classpath
        // (file: and url: prefixes load from files and urls respectively):
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        IniRealm iniRealm = new IniRealm("classpath:shiro.ini");
        securityManager.setRealm(iniRealm);

        // for this simple example quickstart, make the SecurityManager
        // accessible as a JVM singleton.  Most applications wouldn't do this
        // and instead rely on their container configuration or web.xml for
        // webapps.  That is outside the scope of this simple quickstart, so
        // we'll just do the bare minimum so you can continue to get a feel
        // for things.
        SecurityUtils.setSecurityManager(securityManager);

        // Now that a simple Shiro environment is set up, let's see what you can do:

        // get the currently executing user:
        Subject currentUser = SecurityUtils.getSubject();

        // Do some stuff with a Session (no need for a web or EJB container!!!)
        Session session = currentUser.getSession();
        session.setAttribute("someKey", "aValue");
        String value = (String) session.getAttribute("someKey");
        if (value.equals("aValue")) {
            log.info("Retrieved the correct value! [" + value + "]");
        }

        // let's login the current user so we can check against roles and permissions:
        if (!currentUser.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
            token.setRememberMe(true);
            try {
                currentUser.login(token);
            } catch (UnknownAccountException uae) {
                log.info("There is no user with username of " + token.getPrincipal());
            } catch (IncorrectCredentialsException ice) {
                log.info("Password for account " + token.getPrincipal() + " was incorrect!");
            } catch (LockedAccountException lae) {
                log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                        "Please contact your administrator to unlock it.");
            }
            // ... catch more exceptions here (maybe custom ones specific to your application?
            catch (AuthenticationException ae) {
                //unexpected condition?  error?
            }
        }

        //say who they are:
        //print their identifying principal (in this case, a username):
        log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");

        //test a role:
        if (currentUser.hasRole("schwartz")) {
            log.info("May the Schwartz be with you!");
        } else {
            log.info("Hello, mere mortal.");
        }

        //test a typed permission (not instance-level)
        if (currentUser.isPermitted("lightsaber:wield")) {
            log.info("You may use a lightsaber ring.  Use it wisely.");
        } else {
            log.info("Sorry, lightsaber rings are for schwartz masters only.");
        }

        //a (very powerful) Instance Level permission:
        if (currentUser.isPermitted("winnebago:drive:eagle5")) {
            log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
                    "Here are the keys - have fun!");
        } else {
            log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
        }

        //all done - log out!
        currentUser.logout();

        System.exit(0);
    }
}	

三大对象:

  1. Subject
  2. SecurityManage
  3. realm

shiro整个流程

常用过滤器如下

  • anon:无需认证访问
  • authc:必须认证了才能访问
  • user:记住我开启了,才可以用
  • perms:拥有对某个资源的权限才能访问
  • role:该资源必须得到角色权限才可以访问

代码实战:

首先是shiroconfig,我们从下往上配置:

  1. 首先是创建一个realm类
  2. 之后是创建shiroconfig
  3. 之后从下往上配置
    1. 首先是引入realm类
    2. 配置安全管理器
    3. 之后设置过滤工厂
package com.hyc.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

@Configuration
public class shrioconfig {
//    shirofilterfactoryBean
     @Bean
        public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("SecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
            ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
            //设置安全管理器
            bean.setSecurityManager(defaultWebSecurityManager);
            /*
            * 常用过滤器如下
            * anon:无需认证访问
            * authc:必须认证了才能访问
            * user:记住我开启了,才可以用
            * perms:拥有对某个资源的权限才能访问
            * */
            * */
         Map<String,String> filter = new LinkedHashMap();
         filter.put("/add","anon");
         filter.put("/upd","authc");
         bean.setFilterChainDefinitionMap(filter);
         bean.setLoginUrl("/tologin"); 
         return bean;
        }
//    dafultwebSecurityManager
    @Bean(name="SecurityManager")
public DefaultWebSecurityManager getdefaultWebSecurityManager(@Qualifier("Userrealm") userrealm userrealm){
    DefaultWebSecurityManager SecurityManager = new DefaultWebSecurityManager();
//    关联Userrealm
    SecurityManager.setRealm(userrealm);
    return  SecurityManager;

}
    //    创建realm对象,需要自定义类
    @Bean
    public userrealm Userrealm() {
    return new userrealm();
    }
}

realm对象需要引用外面的类 userrealm,我们需要继承AuthorizingRealm来获得授权,认证方法

package com.hyc.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;

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;
    }
}

用户认证:

我们需要去编写config类,设置权限,什么路径需要什么权限,

  @Bean
        public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("SecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
            ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
            //设置安全管理器
            bean.setSecurityManager(defaultWebSecurityManager);
            /*
            * 常用过滤器如下
            * anon:无需认证访问
            * authc:必须认证了才能访问
            * user:记住我开启了,才可以用
            * perms:拥有对某个资源的权限才能访问
            role:该资源必须得到角色权限才可以访问
            * */
         Map<String,String> filter = new LinkedHashMap();
         filter.put("/user/add","perms[user:add]");
         filter.put("/user/upd","perms[user:upd]");
         bean.setFilterChainDefinitionMap(filter);
         bean.setLoginUrl("/tologin");
         bean.setUnauthorizedUrl("/unauth");
         return bean;
        }

之后去realm去认证,认证的信息是我们从数据库user表中查询出来的数据

  //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        System.out.println("认证=========>");
//        获取当前的用户
        Subject subject = SecurityUtils.getSubject();
//        封装用户数据
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        user user = userService.queryUserByName(token.getUsername());
        if (user==null){
            return null;
        }
        //认证的时候创建用户登陆的session
        Session session = subject.getSession();
                //将用户的属性传入到session中
        session.setAttribute("loginUser",user);
		//如何让我们的user可以全局使用,我们需要设置info中第一个参数为user
        return new SimpleAuthenticationInfo(user,user.getPassword() ,"");
    }

之后再认证之后获取用户对象授权,什么对象可以访问什么页面

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权=========>");
        //授权信息
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //传递用户信息
        Subject subject = SecurityUtils.getSubject();
        user currentUser = (user) subject.getPrincipal();
        //从数据库中获取授权角色
        info.addStringPermission(currentUser.getParms());
        info.addRole("user:add");
        info.addRole("user:upd");
        return info;
    }
登陆功能

controller

    @RequestMapping("/login")
    public String login(String username,String password,Model model){
        //获取角色对象
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try {
            subject.login(token);//判断令牌是否正确
            return "index";
        } catch (UnknownAccountException uae) {//用户名不存在
            model.addAttribute("msg","用户名不存在");
            return "login";
        } catch (IncorrectCredentialsException ice) {//密码不存在
            model.addAttribute("msg","密码错误");
            return "login";
        }
    }

这里有一个拓展可以做:就是密码加密处理

我们这里调用的login()方法会走上面我们配置的一系列流程

整合shiro和thymeleaf

需要的命名空间

xmlns:th="http://www.thymeleaf.org"
xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro"
    

要使用整合我们还需要导入整合包依赖

        <!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro -->
        <dependency>
            <groupId>com.github.theborakompanioni</groupId>
            <artifactId>thymeleaf-extras-shiro</artifactId>
            <version>2.0.0</version>
        </dependency>

之后需要去配置类配置一个新的bean

    //整合ShiroDialect:用来整合shiro thymeleaf
    @Bean
    public ShiroDialect getshiroDialect(){
        return new ShiroDialect();
    }


完成以上步骤就可以在模版引擎上使用shiro了

前端页面内容

<!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</title>
</head>
<body>
<h1>你好</h1>
<span th:text="${msg}"></span>
<!--从session中判断值-->
<div th:if="${session.get('loginUser')==null}">
    <a th:href="@{/tologin}">登录</a>
</div>
<a th:href="@{/logout}">注销</a>
<p th:text="${msg}"></p>
<hr>
    <!--通过shiro中的hasPermission方法,判断登录的用户是否有这个权限,有权限才显示-->
<div shiro:hasPermission="user:add">
    <a th:href="@{/user/add}" >add</a>

</div>
<div shiro:hasPermission="user:upd">
    <a th:href="@{/user/upd}">update</a>
</div>
</body>
</html>

源码

配置相关

shiroconfig

package com.hyc.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

@Configuration
public class shrioconfig {
//    shirofilterfactoryBean
     @Bean
        public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("SecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
            ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
            //设置安全管理器
            bean.setSecurityManager(defaultWebSecurityManager);
            /*
            * 常用过滤器如下
            * anon:无需认证访问
            * authc:必须认证了才能访问
            * user:记住我开启了,才可以用
            * perms:拥有对某个资源的权限才能访问
            * */
         Map<String,String> filter = new LinkedHashMap();
         filter.put("/user/add","perms[user:add]");
         filter.put("/user/upd","perms[user:upd]");
         bean.setFilterChainDefinitionMap(filter);
         bean.setLoginUrl("/tologin");
         bean.setUnauthorizedUrl("/unauth");
         return bean;
        }
//    dafultwebSecurityManager
    @Bean(name="SecurityManager")
public DefaultWebSecurityManager getdefaultWebSecurityManager(@Qualifier("Userrealm") userrealm userrealm){
    DefaultWebSecurityManager SecurityManager = new DefaultWebSecurityManager();
//    关联Userrealm
    SecurityManager.setRealm(userrealm);
    return  SecurityManager;

}
    //    创建realm对象,需要自定义类
    @Bean
    public userrealm Userrealm() {
    return new userrealm();
    }


    //整合ShiroDialect:用来整合shiro thymeleaf
    @Bean
    public ShiroDialect getshiroDialect(){
        return new ShiroDialect();
    }



}

userrealm

package com.hyc.config;

import com.hyc.pojo.user;
import com.hyc.service.userServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

public class userrealm extends AuthorizingRealm {
    @Autowired
    userServiceImpl userService;
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权=========>");
        //授权信息
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //传递用户信息
        Subject subject = SecurityUtils.getSubject();
        user currentUser = (user) subject.getPrincipal();
        //授权角色
        info.addStringPermission(currentUser.getParms());
        info.addRole("user:add");
        info.addRole("user:upd");
        return info;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        System.out.println("认证=========>");
//        获取当前的用户
        Subject subject = SecurityUtils.getSubject();
//        封装用户数据
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        user user = userService.queryUserByName(token.getUsername());
        if (user==null){
            return null;
        }
        Session session = subject.getSession();
        session.setAttribute("loginUser",user);

        return new SimpleAuthenticationInfo(user,user.getPassword() ,"");
    }
}

控制层
package com.hyc.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.session.ProxiedSession;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
public class Mycontroller {
    @RequestMapping({"/","/index"})
    public String toIndex(Model model){
        model.addAttribute("msg","hello");
        return "index";
    }
    @RequestMapping("/user/add")
    public String add(){
        return "user/add";
    }
    @RequestMapping("/user/upd")
    public String upd(){
        return "user/upd";
    }
    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }
    @RequestMapping("/login")
    public String login(String username,String password,Model model){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try {
            subject.login(token);
            return "index";
        } catch (UnknownAccountException uae) {//用户名不存在
            model.addAttribute("msg","用户名不存在");
            return "login";
        } catch (IncorrectCredentialsException ice) {//密码不存在
            model.addAttribute("msg","密码错误");
            return "login";
        }
    }
    @ResponseBody
    @RequestMapping("/unauth")
    public String unauth(){
        return "您没有权限";
    }
    @RequestMapping("/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.removeAttribute("loginUser");
        return "index";
    }
}

前端页面:

index

<!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</title>
</head>
<body>
<h1>你好</h1>
<span th:text="${msg}"></span>
<!--从session中判断值-->
<div th:if="${session.get('loginUser')==null}">
    <a th:href="@{/tologin}">登录</a>
</div>
<a th:href="@{/logout}">注销</a>
<p th:text="${msg}"></p>
<hr>
<div shiro:hasPermission="user:add">
    <a th:href="@{/user/add}" >add</a>

</div>
<div shiro:hasPermission="user:upd">
    <a th:href="@{/user/upd}">update</a>
</div>
</body>
</html>

login

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>登陆</p>
<p th:text="${msg}" style="color: red"></p>
<form th:action="@{/login}">
   <p> 用户名:<input type="text" name="username"></p>
   <p> 密码:<input type="password" name="password"></p>
    <p><input type="submit" value="提交"></p>
</form>
</body>
</html>

安全框架总结:

学习了Springsecurity和shiro之后,总结出一些学习方法

  1. 安全框架的核心思想都十分相似,授权,认证,防伪等
  2. 他们通常都有几个对象,如shiro中的subject,securityManager一样,
  3. 源码的注释写有方法使用的模版,我们可以通过下载源码去查看注释,
  4. Springsercurity和shiro的区别,两个我个人认为,除了一个基于Spring之外功能上两者几乎一致
  5. 使用的感受
    1. Spring Security基于Spring开发,项目中如果使用Spring作为基础,配合Spring Security做权限更加方便,而Shiro需要和Spring进行整合开发
    2. 感觉shiro没有类似于Spring Security那样的安全防护
    3. shiro不需要基于任何框架,依赖性低
    4. 个人认为:配置的麻不麻烦关键在于项目用不用Spring,我看大神们写博客都说shrio配置要更简单一些,但是简单的上手了两个安全框架之后,我觉得使用了Spring的项目上手security要比shiro简单的多,
    5. 还有个个人感想,帮助文档的阅读能力太重要了,学习和接触新技术在没有教程的情况下,文档的阅读能力决定了你的学习上限(个人中间有一段只照着官方文档学习,十分痛苦)
  6. 以上就是安全框架简单上手的全内容啦,
Springboot整合Shiro之授权   本文我们来介绍SpringBoot整合Shiro来实现授权的操作 一、注解的使用方式 1.配置类修改   在权限校验的时候,我们一般在控制中通过注解来实现权限的校验,但我们需要放开注解的使用,在配置文件中的使用方式如下: 对应的在配置类中的设置如下: /** * 开启授权注解使用方式 * @param manager * @return */ @Bean public Authori... 阅读详情

相关推荐

Windows Server 2019整机备份与异机还原实战:从虚拟机到物理机的完整迁移指南

本文详细介绍了Windows Server 2019整机备份与异机还原的实战操作,涵盖从虚拟机到物理机的完整迁移流程。通过环境准备、备份策略设计、具体操作步骤及迁移后优化等内容,帮助用户实现系统环境的无缝迁移,特别适用于服务器硬件升级、数据中心迁移等场景。

weixin_30555125的博客 141

SpringBoot整合并简单使用Shiro详细示例

Shiro的基本知识 + 本次示例的项目代码 + 两分钟测试视频 在本文末给出了下载链接。 软硬件环境:JDK1.8、Eclipse、Shiro1.3.2。 提示 本节知识中,本意是分享前后端分离的Shiro用法的;但是为了更直观,所以本 人把几个需要用到的html文件引入到了项目中;在实际前后端分离时,可以只对后端URI进行Shiro验证,前端是否验证等交给前段工作人员处理即可。 ...

JustryDeng 7908

MOSFET热失效机理与保护电路设计:实战案例

深入解析MOSFET工作原理及其在高温下的失效机制,结合实际案例探讨有效保护电路设计,提升系统可靠性与安全性。

weixin_28713083的博客 858

springboot整合shiro

创建springboot工程 添加配置文件 创建自定义的Relam 创建过滤器 配置shiro管理器 创建controller层 service层 dao层 错误: 修改配置类

weixin_43766390的博客 173

Ribo测序翻译效率计算及可视化

本文介绍了一个计算翻译效率(TE)和分析转录-翻译相关性的生物信息学流程。首先通过Python脚本从标准化表达量矩阵(TPM/FPKM)计算TE值,该脚本自动匹配转录组(RNA)和翻译组(Ribo)样本进行比值运算。然后使用R语言进行相关性分析,通过ggplot2绘制样本间转录-翻译表达量的散点图,添加线性回归线和相关系数标注,支持PNG/PDF格式输出。整个流程实现了从原始数据处理到可视化分析的全过程。

Tiaoxiaobai的博客 564

YOLO26涨点改进 | 小目标损失函数独家改进篇 | TGRS 2025 | 引入AdaFocalLoss损失函数,改进小目标检测,自适应聚焦小目标与样本分布不平衡,提升检测精度与训练稳定性

目录一、前言:YOLO26原生损失函数在小目标检测中的核心痛点(涨点瓶颈)1.1 小目标权重不足,聚焦能力薄弱1.2 样本分布不平衡适配性差,正负样本权重失衡1.3 训练不稳定,易出现梯度消失/震荡1.4 未区分小目标与背景的冗余干扰,误检率高二、TGRS 2025顶刊灵感解析:自适应聚焦损失的核心逻辑三、全网独家创新:AdaFocalLoss损失函数设计与YOLO26适配(核心重点,可复现)3.1 AdaFocalLoss函数整体设计(独家设计,清晰可复现)3.2 各核心模块详细设计(独家创新,可直接复现

qq_36130719的博客 215

一.springboot与shiro整合(示例)

地址: 演示地址:http://47.98.153.30:8080 账号:melo 密码:12345678 github地址:点击打开链接https://github.com/MeloFocus/focus 前端水平有限见谅 第一个整合目标: (1)用springboot整合shiro (2)完成简单的登录功能 (3)对url进行权限控制,当前登录用户拥有此ur...

u014203449的博客 4万+

毕设-MobileNetV4:给YOLOv8装上轻量化“引擎”——通用瓶颈与C2f融合的实战教程

本文介绍了一种将MobileNetV4与YOLOv8结合的目标检测模型轻量化方案。MobileNetV4通过通用瓶颈(UB)结构实现多任务适配,其核心改进包括动态卷积核、注意力机制和多路径特征聚合,显著提升特征提取能力。作者详细阐述了如何在YOLOv8框架中实现MobileNetV4的通用瓶颈模块,包括"膨胀-卷积-注意力-压缩"的流程设计,并构建完整的backbone网络。该方案通过融合MobileNetV4的轻量化优势与YOLOv8的高效检测架构,在移动设备上实现了精度与速度的良好平

FJN110的博客 516

SpringBoot整合Shiro示例

Shiro这个框架呢,对我来说真的相见恨晚,一直想用,但没机会用。 这个Shiro我也不多做介绍了,本文也只是简单应用,供大家学习参考。 本文有点长,我写了小半天..有点耐心小伙子! 本文涉及Shiro知识点:自定义Realms,Shiro加密,登录验证,权限验证,前端Shiro标签,Shiro注解。 Shiro的三大核心组件 Subject SecurityMan...

954L 607

ProcessingJoy —— 最简单的实时流体模拟【JAVA】

实现基于非常非常经典的 Paper ——Jos Stam 的Real-Time Fluid Dynamics for Games 代码和注释 float visc = 0.0f; float diff = 0.0f; int N = 250; int size = (N + 2) * (N + 2); float[] u = new float[size]; // 速度, 水平/竖直 float[] v = new float[size]; float[] u_prev = new fl...

ShaderJoy 的兴趣技术杂货铺 2822

STM32G4 电机外设篇(三) TIM1 发波 和 ADC COMP DAC级联

STM32G4电机控制外设应用笔记摘要:本文详细介绍了TIM1高级定时器生成三对互补PWM波的配置方法(预分频器PSC=30000,重装载值ARR=10000),重点说明需断开电机连线避免干扰。通过STM32CubeMX配置TIM1通道4触发ADC采样,设置RCR分频参数,并给出Keil代码修改要点。第二部分探讨了TIM1、ADC、比较器和DAC的级联应用,包括电流采样触发时序(在PWM波峰值前采样)和过流保护机制。配置步骤包含DAC3作为比较器负输入端、设置Break触发封波保护,并附关键代码段实现外设联

qq_43091070的博客 1108

spring boot整合shiro(附带简单demo)

shiro是目前主流的java安全框架,主要用来更便捷的认证,授权,加密,会话管理。废话不多说,下面是一个简单的案例,Soring boot整合shiro;首先搭建一个spring boot框架,具体步骤请参考点击打开链接;然后配置shiro依赖(顺便整合mybatis)               &lt;dependency&gt; &lt;groupId&gt;org.springfra...

诗简默的博客 1万+

【OpenClaw从入门到精通】第15篇:国产芯片适配实战——昇腾/龙芯/天数智芯全流程跑通(2026实测版)

2026年国产芯片AI适配迎来爆发期,OpenClaw作为轻量化本地AI智能体,已实现华为昇腾、龙芯、天数智芯等主流国产平台的全流程适配。本文基于实测数据,从背景解析、核心原理、算法优化、实操部署四个维度,详细讲解OpenClaw在香橙派昇腾开发板、龙芯3B6000M、天数彤央TY1200及联想开天X7h的部署流程,同时提供统一工具玄武CLI的使用指南。文中包含完整代码、执行结果、避坑技巧及虚拟实战案例,兼顾新手入门与进阶提升,帮助读者快速掌握国产芯片上的本地AI部署能力,实现数据隐私保护与低成本AI落地。

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 1024

SpringBoot2.0集成Shiro

最近搞了下shiro安全框架,网上找了好多篇博客,感觉要么都是复制粘贴,要么就是错误百出。至于稍微讲解一下为什么要这么做,就更别说了。这篇文章就教大家如何将 Shiro 整合到 SpringBoot 中,并且避开一些小坑,由浅入深,从最基本的配置开始,一步一步加入新的功能。这样理解起来也稍微简单点 项目版本: springboot2.x shiro:1.3.2 Maven配置: ...

我是你妹她哥的博客 9万+

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

springboot整合shiro(超详细,你想要的都在这了)

Springboot整合Shiro 文章目录pom依赖前端页面(thymeleaf整合shiro)thymeleaf中shiro标签解释数据库理解shiro的几个组成部分编写Shiro配置类 pom依赖 <dependencies> <!--thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId>

GOV_D的博客 1万+

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

Springboot整合Shiro框架入门

Springboot中集成Shiro框架1、快速开始demo2、Springboot集成Shiro2.1、环境搭建2.2、shiro的拦截2.3、shiro的认证2.4、整合mybatis2.5、shiro进行授权2.6、shiro整合thymeleaf 1、快速开始demo 来到shiro的官网:shiro官方网站: 下载官方在git仓库上提供的源码,下载后解压: 官方提供了在多种场景下使用的demo,等一下会用到sample里面的东西。下面直接按官方推荐的10分钟入门demo进行测试。 创建一

qq_42640067的博客 2154

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Springboot整合shiro

是一款主流的Java安全框架,不依赖任何容器,可以运行在Java SE和Java EE项目中,它的主要作用是对访问系统的用户进行身份认证、授权、会话管理、加密等操作。Shiro 就是用来解决安全管理的系统化框架。Shiro是基于session的身份认证和访问控制框架。RBAC是Role-Based Access Control(基于角色的访问控制)的缩写。它是一种广泛应用于安全管理中的访问控制模型。RBAC模型通过授予用户不同的角色,并将角色与权限进行关联,来管理对资源的访问。

chao的博客 2800

SpringBoot整合Shiro框架

目录 1、Shiro简介 1.1、Shiro是什么? 1.2、有哪些功能? 1.3、Shiro架构(外部) 2、快速入门(QuickStartShiro) 2.1、项目结构 2.2、导入shiro依赖,这是我的pom所有依赖。(pom.xml) 2.3、相关配置文件 (1)log4j.properties——官网 (2)shiro.ini——官网 (3)启动类 Quickstart——官网 3、SpringBoot集成Shiro框架 3.1SpringBoot整合Shir...

koko创作 8920

SpringBoot与Shiro整合

本文章介绍了Spring boot下shiro与thymeleaf、mybatis的整合过程,Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理

fangliangke的博客 6522

SpringBoot整合shiro整合thymeleaf详细示例

SpringBoot整合shiro+thymeleaf详细示例准备工作:1.所需的maven依赖2.搭建测试项目2.1创建后台结构和所需类2.2 创建数据库2.3 编写配置文件2.4 编写pojo,dao,service包下的类方法2.5 controller层以及前端页面3.编写核心配置类开始测试: 这是一片从头到尾的详细整合演示,让你直接复制就能运行的代码。 准备工作: 1.所需的maven依...

chengweimam的博客 1596

Spring Boot 整合 Shiro 快速入门

Spring Boot 整合 Shiro 快速入门1 Spring Boot 快速集成 Shiro 示例1.1 新建 Spring Boot Maven 示例工程项目1.2 依赖引入 Pom.xml1.3 配置 application.yml1.4 自定义 Realm 领域 UserRealm 实现自定义认证与授权1.6 shiro 实现登录认证1.6.1 登录 html 页面1.6.2 登录逻辑...

fishpro的博客 1744

SpringBoot入门实战:SpringBoot整合Shiro

SpringBoot是Spring公司推出的一款快速开发框架,它可以帮助开发者快速搭建Spring应用程序,同时提供了许多内置的功能,如数据库连接、缓存、日志等。Shiro是一个基于Java的安全框架,它提供了身份验证、授权、会话管理等功能。在本文中,我们将介绍如何将SpringBoot与Shiro整合,以实现安全的应用程序开发。

2403_88996764的博客 1245

springboot整合shiro实现登录验证

springboot整合shiro实现登录验证 今天第一次接触springboot,本来是要学习springboot和shiro整合的,但是由于springboot结构还不太了解,所以先来了解一下springboot。 springboot可以快速创建一个机遇spring的项目,而且让这个项目跑起来只需要很少的配置就可以了,主要有以下核心功能: 1.独立运行的spring项目:springboo...

c1225992531的博客 8560
上一篇: swagger:快速入门
下一篇: Springboot:任务使用详解
冷环渊
博客等级 码龄7年 1130粉丝 176原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冷环渊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值