User-Profile-Image
hankin
  • 5
  • 首页
  • 技术-Study
    • Java
    • Php
    • WordPress
  • Enjoy-Life
  • 随拍-Photo
  • 分享-Share
  • 关于我
  • 分类
    • 随拍-Photo
    • Enjoy-Life
    • 技术-Study
    • 所有
    • 分享-Share
    • Java
    • Wordpress
    • Php
    • SQL Server
    • Linux
  • 页面
    • 首页
    • 学习日志
    • 生活日记
    • 随手拍拍
    • 关于我
  • 友链
    • 百度一下
    • 我爱水煮鱼
    • Jay's Notes
    • 淮城一只猫
    • 雨林寒舍
    • 不败君
    • 广告联盟
    • hankin
    • Tengine
    • 寥寥尘事 尘事固已矣,秉意终不迁。
    • 友人C
    • Typecho
    • jokeWorld
    • 蓝洛水深
    • 身边的大佬
    • Duilib
    • Runoob
    • Hank
Help?

Please contact us on our email for need any support

Support
    首页   ›   技术-Study   ›   Java   ›   正文
Java

Shiro(安全)-Springboot环境搭建

2021-03-08 23:03:56
318  0 0

IDEA 快捷键 //ctrl+alt+T 自动弹出try catch

1、导入shiro的jar包

<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.7.1</version>
</dependency>

百度搜索 shiro-spring maven

https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring/1.7.1

2、两大核心配置 config

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

@Configuration
public class ShiroConfig {

    //ShiroFilterFactoryBean:第3步
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);
        return bean;
    }
    //现在就是第一步和第二步绑定起来了 通过@Qualifier("userRealm") UserRealm userRealm
    //DefaultWebSecurityManager:第2步
    @Bean("securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联UserRealm
        securityManager.setRealm(userRealm);
        return securityManager;
    }
    //创建 realm 对象 , 需要自定义类:第1步 或者@Bean(name="userRealm") 这个name就是第二步Qualifier处的名字
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

}

package com.kuang.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 extends AuthorizingRealm
public class UserRealm extends AuthorizingRealm {
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了=>授权doGetAuthorizationInfo");
        return null;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行了=>认证doGetAuthenticationInfo");
        return null;
    }
}

加入认证

//添加shiro的内置过滤器
        /*
             anon:无需认证就可以访问
             authc:必须认证了才能访问
             user: 必须拥有 记住我 功能才能用
             perms:拥有对某个资源的权限才嗯那个访问
             role:拥有某个角色权限才能访问
        */
        Map<String, String> filerMap = new LinkedHashMap<>();
        filerMap.put("/user/add","authc");
        filerMap.put("/user/update","authc");
        bean.setFilterChainDefinitionMap(filerMap);

登陆拦截

/*
             anon:无需认证就可以访问
             authc:必须认证了才能访问
             user: 必须拥有 记住我 功能才能用
             perms:拥有对某个资源的权限才嗯那个访问
             role:拥有某个角色权限才能访问
            //filerMap.put("/user/add","authc");
            //filerMap.put("/user/update","authc");
        */
        //登陆拦截
        Map<String, String> filerMap = new LinkedHashMap<>();
        filerMap.put("/user/*","authc"); //通配符*
        bean.setFilterChainDefinitionMap(filerMap);
        //设置登录的请求
        bean.setLoginUrl("/toLogin");

controller/MyController

package com.kuang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyController {

    @RequestMapping({"/","/index","/index.html"})
    public  String toIndex(Model model){
        model.addAttribute("msg","hello,Shiro");
        return "index";
    }
    @RequestMapping("/user/add")
    public String add(){
        return "user/add";
    }
    @RequestMapping("/user/update")
    public String update(){
        return "user/update";
    }
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

}

首页

<!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>
<hr>
<a th:href="@{/user/add}">add</a> | <a th:href="@{/user/update}">update</a>
</body>
</html>

登陆页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>登陆</h1>
<form action="">
    <p>用户名:<input type="text" name="username"></p>
    <p>密码:<input type="text" name="password"></p>
    <p><input type="submit"></p>
</form>
</body>
</html>
shiro-thymeleaf整合包
https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro/2.0.0
<!--shiro-thymeleaf整合包-->
<!-- 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>
近期文章
  • Docsify 3分钟搭建博客
  • electron -v 全局安装,报错到晕厥
  • Electron-vue初始化项目时候报错:Command vue init requires a global addon to be installed.
  • 电脑重启后Visual Studio Code的快捷方式不存在?
  • 搜狗浏览器提示:您要访问的网站包含大量违法或违规内容
标签
0x800706be 18456 Apache bbr CDN CentOS command not found Dubbo GCE GCP google Google Cloud Platform IDEA K2 Memcached microsoft sql native client Mysql nano ODBC PB PHP phpstudy Powerbuilder Python shiro SQL2008 SQL Server Sqlite sqlite_master Sql server 2008 R2 sqlstate=08001 SSL ssr SyncPragma table thymeleaf Wordpress zookeeper 中石化,加油卡,百度地图 字段 客户端不支持加密 幽默 数据库 正则表达式 登录失败 腾讯CDN
Copyright © 2023 网站备案号: 苏ICP备18047535号
主页
页面
  • 首页
  • 学习日志
  • 生活日记
  • 随手拍拍
  • 关于我
博主
_陈默默 管理员
人生就是:定性,知事,选梦,遇人,择城,终老。
125 文章 1 评论 67724 浏览
测试
测试