SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx

上传人:b****1 文档编号:3113861 上传时间:2023-05-01 格式:DOCX 页数:45 大小:804.71KB
下载 相关 举报
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第1页
第1页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第2页
第2页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第3页
第3页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第4页
第4页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第5页
第5页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第6页
第6页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第7页
第7页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第8页
第8页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第9页
第9页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第10页
第10页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第11页
第11页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第12页
第12页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第13页
第13页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第14页
第14页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第15页
第15页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第16页
第16页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第17页
第17页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第18页
第18页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第19页
第19页 / 共45页
SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx_第20页
第20页 / 共45页
亲,该文档总共45页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx

《SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx》由会员分享,可在线阅读,更多相关《SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx(45页珍藏版)》请在冰点文库上搜索。

SpringBoot整合Shiro搭建权限管理组织系统Word文档格式.docx

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

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2