springboot shiro 基本整合
springboot shiro 基本整合
https://www.w3cschool.cn/shiro/c52r1iff.html
http://shiro.apache.org/configuration.html#Configuration-ProgrammaticConfiguration
步骤
依赖和配置
定义 org.apache.shiro.realm.Realm 使用 IniRealm
定义 org.apache.shiro.mgt.SecurityManager
定义 org.apache.shiro.spring.web.ShiroFilterFactoryBean
指定登录页面 shiroFilterFactoryBean.setLoginUrl("/login.html")
放行登录相关接口和静态资源
定义登录Controller
Map<String, String> map = new HashMap<>();
map.put("/login/pc", "anon");
shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
配置
server:
servlet:
context-path: /mozq
# user.ini
[users]
liubei=123
添加Bean
package com.mozq.sb.shiro03.config;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.shiro.mgt.SecurityManager;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
//自定义 org.apache.shiro.realm.Realm
@Bean
public IniRealm iniRealm(){
IniRealm iniRealm = new IniRealm("classpath:user.ini");
return iniRealm;
}
//定义 org.apache.shiro.mgt.SecurityManager
@Bean
public SecurityManager securityManager(Realm realm){
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(realm);
return securityManager;
}
//定义 org.apache.shiro.spring.web.ShiroFilterFactoryBean
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
//过滤的路径
Map<String, String> map = new HashMap<>();
map.put("/**", "authc");
map.put("/login/pc", "anon");//放行登录相关接口
map.put("/js/jquery-3.4.1.js", "anon");//放行登录页面静态资源,不然登录页报错。
shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
shiroFilterFactoryBean.setLoginUrl("/login.html");//登录页面
return shiroFilterFactoryBean;
}
}
登录页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-3.4.1.js"></script>
</head>
<body>
<form action="/login/pc" method="post">
<input id="username" name="username">
<input id="password" name="password">
<!-- <input type="button" value="登录"> -->
<input type="button" value="登录" onclick="login()">
</form>
<script>
var serverPath = "/mozq";
function login() {
var username = $("#username").val();
var password = $("#password").val();
console.log(username);
console.log(password);
$.ajax({//Content-Type: application/x-www-form-urlencoded; charset=UTF-8
url: serverPath + "/login/pc",
type: "post",
data: {"username": username, "password": password},
success: function (res) {
if(res === "success"){
window.location.href = "http://www.baidu.com";
}else{
alert(res);
}
}
});
}
</script>
</body>
</html>
登录Controller
package com.mozq.sb.shiro03.controller;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/login")
public class LoginController {
@RequestMapping("/pc")
public String login(@RequestParam String username, @RequestParam String password){
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);
try {
subject.login(usernamePasswordToken);
} catch (AuthenticationException e) {
e.printStackTrace();
return "身份验证错误";
}
return "success";
}
}
bugs
1.
Caused by: org.apache.shiro.config.ConfigurationException: java.io.IOException: Resource [classpath: user.ini] could not be found.
原因:[classpath: user.ini] 路径中加了空格
2.
不配置登录跳转页面,则默认跳转到 http://localhost:8080/login.jsp
配置登录接口:shiroFilterFactoryBean.setLoginUrl("/login.html");//则这个接口成为放行的,即anon
如果项目路径为: localhost/mozq
则此时登录跳转到:localhost/mozq/login.html
3.
如果登录相关的其他接口不放行,则重定向到登录页面。
放行登录相关的接口:map.put("/login/pc", "anon");
如果项目路径为: localhost/mozq
则此时放行路径:localhost/mozq/login/pc
4.
org.apache.shiro.authc.UnknownAccountException: Realm [org.apache.shiro.realm.text.IniRealm@4c9f4ee9] was unable to find account data for the submitted AuthenticationToken [org.apache.shiro.authc.UsernamePasswordToken - 1, rememberMe=false].
原因:user.ini中什么都没有写。则登录验证失败。
方案:添加用户信息
[users]
liubei=123
5.
Uncaught SyntaxError: Unexpected token <
原因:shiro 将login.html的资源 jquery-3.4.1.js 拦截了,无法访问,所以报了这个错误。
方案:
放行静态资源
map.put("/js/jquery-3.4.1.js", "anon")
springboot shiro 基本整合的更多相关文章
- SpringBoot+Shiro+mybatis整合实战
SpringBoot+Shiro+mybatis整合 1. 使用Springboot版本2.0.4 与shiro的版本 引入springboot和shiro依赖 <?xml version=&q ...
- springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3. springboot+shiro+redis(集群re ...
- springboot+shiro+redis(集群redis版)整合教程
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3.springboot+shiro+redis(单机red ...
- springboot+shiro+redis(单机redis版)整合教程
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(集群redis版)整合教程 3.springboot+shiro+redis(单机red ...
- springboot+shiro整合教程
进阶教程: 1. springboot+shiro+redis(单机redis版)整合教程 2. springboot+shiro+redis(集群redis版)整合教程 3.springboot+s ...
- springboot+shiro
作者:纯洁的微笑 出处:http://www.ityouknow.com/ 这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公 ...
- Spring Cloud之路:(七)SpringBoot+Shiro实现登录认证和权限管理
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sage_wang/article/details/79592269一.Shiro介绍1.Shiro是 ...
- SpringBoot&Shiro实现权限管理
SpringBoot&Shiro实现权限管理 引言 相信大家前来看这篇文章的时候,是有SpringBoot和Shiro基础的,所以本文只介绍整合的步骤,如果哪里写的不好,恳请大家能指出错误,谢 ...
- SpringBoot&Shiro实现用户认证
SpringBoot&Shiro实现用户认证 实现思路 思路:实现认证功能主要可以归纳为3点 1.定义一个ShiroConfig配置类,配置 SecurityManager Bean , Se ...
随机推荐
- vbs与其他语言进行交互编程(外存传参)
vbs没有自定义排序函数.无需自己造轮子,可以用其他语言来完成这个任务(在传递数据比较简单的情况下,例如只传递数组). 首先用5分钟写一个C++排序的代码.命名为“mysort.cpp”: #incl ...
- mysql不是内部或外部命令--windows环境下报错的解决
安装Mysql后,当我们在cmd中敲入mysql时会出现‘Mysql’不是内部或外部命令,也不是可运行的程序或其处理文件. 处理: 我的电脑右键属性>高级系统设置>高级>环境变量&g ...
- 字节跳动笔试题:1. 小于N的质数数量;2. 逆时针二维数组;3. 判断a+b>c
1. 小于N的质数数量 import java.util.Scanner; /** * 计算小于N的质数数量 * @author Turing * */ public class Main4 { pu ...
- nodejs插件化框架——minimajs
本文介绍我开发的一个JavaScript编写的插件化框架——minimajs,完全开源,源码下载地址:https://github.com/lorry2018/minimajs.该框架参考OSGi规范 ...
- oracle聚合函数XMLAGG用法简介
XMLAGG函数语法基本如图,可以用于列转行,列转行函数在oracle里有好几种方法,wm_concat也可以做 这里介绍wm_concat是因为XMLAGG实现效果和wm_concat是一样的,只是 ...
- [LeetCode#178]Rank Scores
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- CodeForce 222C Reducing Fractions
To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractio ...
- .NET Core 学习笔记之 WebSocketsSample
1. 服务端 代码如下: Program: using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; namespace WebS ...
- 基于cephfs搭建高可用分布式存储并mount到本地
原文:https://www.fullstackmemo.com/2018/10/11/cephfs-ha-mount-storage/ 服务器硬件配置及环境 项目 说明 CPU 1核 内存 1GB ...
- EF CodeFirst 使用T4模板
实用等级:★★★★★ 首先,定义一个接口,代表一个领域实体.在定义一个实体集成这个接口,面向接口编程的各种好处就不提了. /// <summary> /// 代表一个领域实体 /// &l ...