SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx
《SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx》由会员分享,可在线阅读,更多相关《SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx(45页珍藏版)》请在冰点文库上搜索。
![SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/1/3eee7d3f-3ed8-454f-ad22-7fc15af517b6/3eee7d3f-3ed8-454f-ad22-7fc15af517b61.gif)
4.添加springboot启动器(web支持)
--web支持-->
dependency>
<
spring-boot-starter-web<
/dependency>
完整的pom.xml文件如下:
projectxmlns="
http:
//maven.apache.org/POM/4.0.0"
xmlns:
xsi="
//www.w3.org/2001/XMLSchema-instance"
xsi:
schemaLocation="
//maven.apache.org/POM/4.0.0http:
//maven.apache.org/xsd/maven-4.0.0.xsd"
>
modelVersion>
4.0.0<
/modelVersion>
com.hellotomcat<
springboot-shiro<
0.0.1-SNAPSHOT<
dependencies>
--thymeleaf-->
spring-boot-starter-thymeleaf<
/dependencies>
--修改参数-->
properties>
--修改thymeleaf的版本-->
thymeleaf.version>
3.0.2.RELEASE<
/thymeleaf.version>
thymeleaf-layout-dialect.version>
2.0.4<
/thymeleaf-layout-dialect.version>
/properties>
/project>
5.编写controller(UserController)
packagecom.hellotomcat.controller;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.Model;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.ResponseBody;
@Controller
publicclassUserController{
/***
*测试方法
*@return
*/
@RequestMapping("
/hello"
)
@ResponseBody//返回json数据
publicStringhello(){
System.out.println("
hellospringboot"
);
return"
ok"
;
}
*测试thymeleaf
*@parammodel
/testThymeleaf"
publicStringtestThymeleaf(Modelmodel){
//把数据放入model
model.addAttribute("
name"
"
admin"
//返回test.html
test"
}
6.编写启动类Application
packagecom.hellotomcat;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
/***
*SpringBoot启动类
*@authorLenovo
*
@SpringBootApplication
publicclassApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(Application.class,args);
7.运行启动类Application(和运行普通的Java程序一样)
8.然后在浏览器输入:
//localhost:
8080/hello,就可以正常访问了,出现如下画面说明启动成功
二、导入thymeleaf页面模块
1.引入thymeleaf依赖
2.在controller当中添加如下方法:
3.在src/main/resources目录下面建立templates目录用来存放页面(Spting-Boot默认页面存放路径,名字不可更改)
4.在templates目录下新建test.html
DOCTYPEhtml>
html>
head>
title>
测试thymeleaf的使用<
/title>
metaname="
keywords"
content="
keyword1,keyword2,keyword3"
/>
description"
thisismypage"
content-type"
text/html;
charset=UTF-8"
--<
linkrel="
stylesheet"
type="
text/css"
href="
./styles.css"
-->
/head>
body>
h3th:
text="
${name}"
/h3>
/body>
/html>
th:
为thymeleaf语法,获取model中传过来的值
5.在浏览器访问http:
8080/testThymeleaf进行测试.如果能够在页面上获取到值就说明成功了.
此处需要注意在thymeleaf3.0以前对页面标签语法要求比较严格,开始标签必须有对应的结束标签,如果没有就出现如下错误.
如果页面标签不严谨还希望使用thymeleaf的话,那就需要升级thymeleaf到3.0以上的版本,此处升级为3.0.2
6.升级thymeleaf版本(修复上面的错误),在properties节点下面添加
三、SpringBoot与Shiro整合实现用户认证
1.Shiro核心API类
Subject:
用户主体(把操作交给SecurityManager)
SecurityManager:
安全管理器(关联Realm)
Realm:
shiro连接数据库的桥梁
2.导入shiro与spring整合依赖
--shiro与Spring整合依赖-->
org.apache.shiro<
shiro-spring<
1.4.0<
3.创建自定义Realm
packagecom.hellotomcat.shiro;
importorg.apache.shiro.authc.AuthenticationException;
importorg.apache.shiro.authc.AuthenticationInfo;
importorg.apache.shiro.authc.AuthenticationToken;
importorg.apache.shiro.authz.AuthorizationInfo;
importorg.apache.shiro.realm.AuthorizingRealm;
importorg.apache.shiro.subject.PrincipalCollection;
*自定义Realm
publicclassUserRealmextendsAuthorizingRealm{
*执行授权逻辑
@Override
protectedAuthorizationInfodoGetAuthorizationInfo(PrincipalCollectionarg0){
执行授权逻辑"
returnnull;
*执行认证逻辑
protectedAuthenticationInfodoGetAuthenticationInfo(AuthenticationTokenarg0)throwsAuthenticationException{
执行认证逻辑"
4.编写shiro的配置类(重点)(最基础的配置类如下)
importorg.apache.shiro.spring.web.ShiroFilterFactoryBean;
importorg.apache.shiro.web.mgt.DefaultWebSecurityManager;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
*Shiro的配置类
@Configuration
publicclassShiroConfig{
*创建ShiroFilterFactoryBean
publicShiroFilterFactoryBeangetShiroFilterFactoryBean(@Qualifier("
securityManager"
)DefaultWebSecurityManagersecurityManager){
ShiroFilterFactoryBeanshiroFilterFactoryBean=newShiroFilterFactoryBean();
//设置安全管理器
shiroFilterFactoryBean.setSecurityManager(securityManager);
returnshiroFilterFactoryBean;
*创建DefaultWebSecurityManager
@Bean(name="
publicDefaultWebSecurityManagergetDefaultWebSecurityManager(@Qualifier("
userRealm"
)UserRealmuserRealm){
DefaultWebSecurityManagersecurityManager=newDefaultWebSecurityManager();
//关联realm
securityManager.setRealm(userRealm);
returnsecurityManager;
*创建Realm
@Bean
publicUserRealmgetRealm(){
returnnewUserRealm();
5.使用shiro内置过滤器实现拦截功能
1.
2.
3.
4.
5.
5.1.新建两个页面add.html和update.html
add.html页面代码:
用户新增页面<
用户新增
update.html页面代码:
用户更新页面<
用户更新
5.2.修改test.html页面
br>
进入用户添加功能:
ahref="
add"
用户添加<
/a>
进入用户更新功能:
update"
用户更新<
5.3.在UserController当中添加下面的方法
@RequestMapping("
/add"
//没有@ResponseBody这个注释则返回页面,有就返回json数据
publicStringadd(){
/user/add"
/update"
publicStringupdate(){
/user/update"
5.4.修改ShiroConfig类
importjava.util.LinkedHashMap;
importjava.util.Map;
//添加Shiro内置过滤器
/***
*Shiro内置过滤器,可以实现权限相关的拦截
*常用的过滤器:
*anon:
无需认证(登录)可以访问
*authc:
必须认证才可以访问
*user:
如果使用rememberMe的功能可以直接访问
*perms:
该资源必须得到资源权限才可以访问
*role:
该资源必须得到角色权限才可以访问
*/
Map<
String,String>
filterMap=newLinkedHashMap<
();
filterMap.put("
authc"
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
userRe