Hello Mr.J——shiro+springmvc
来源:互联网 发布:淘宝网店需要交税吗 编辑:程序博客网 时间:2024/05/01 07:17
上次写完了shiro的博客也是做了很多的工作,项目中的shiro结合了很多东西使用,整个配置文件有些混乱,现在才算是理清楚了。
那么我们先看看shiro的基本功能如何和springmvc相结合的。
我们的项目是maven项目,所以先在pom文件中加入shiro需要的包。
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-web --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.3.0</version> </dependency>然后在我们的web.xml或者applicationContext.xml中添加shiro的拦截器。
<filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>添加一个spring配置文件,例如我这里叫applicationContext-shiro.xml,内容是配置shiro的简单信息。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-lazy-init="true"> <!-- 用于扫描其他的.properties配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:shiro.properties</value> </list> </property> </bean> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!-- Shiro的核心安全接口 --> <property name="securityManager" ref="securityManager"/> <!-- 要求登录时的链接(登录页面地址),非必须的属性,默认会自动寻找Web工程根目录下的"/login.jsp"页面 --> <property name="loginUrl" value="${loginUrl}" /> <!-- 登录成功后要跳转的连接(一般可以在Controller中进行处理) --> <property name="successUrl" value="${SuccessUrl}" /> <!-- 用户访问未对其授权的资源时,所显示的连接 --> <property name="unauthorizedUrl" value="${UnauthorizedUrl}"/> <property name="filterChainDefinitions"> <value> <!--不拦截login的url--> /login=anon /logout=logout /**=user </value> </property> </bean> <!--shiro核心管理类,这里只配置了自己的realm--> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="shiroRealm"/> </bean> <!-- shiro于数据交互的类 ,自己写的类的实现-ShiroRealmBean自己重写的类的实现 --> <bean id="shiroRealm" class="com.tgb.shiro.realm.shiroRealm"/></beans>这里面比较不容易理解的就是url的拦截格式了,这里叫做shiro的拦截器链,这拦截器链可以使用我们自己的拦截器,也可以用shiro自带的一些处理拦截器。
我这里有个别人整理好的表格,顺手就用一下吧。
默认拦截器名
拦截器类
说明(括号里的表示默认值)
身份验证相关的
authc
org.apache.shiro.web.filter.authc
.FormAuthenticationFilter
基于表单的拦截器;如“/**=authc”,如果没有登录会跳到相应的登录页面登录;主要属性:usernameParam:表单提交的用户名参数名( username); passwordParam:表单提交的密码参数名(password); rememberMeParam:表单提交的密码参数名(rememberMe); loginUrl:登录页面地址(/login.jsp);successUrl:登录成功后的默认重定向地址; failureKeyAttribute:登录失败后错误信息存储key(shiroLoginFailure);
authcBasic
org.apache.shiro.web.filter.authc
.BasicHttpAuthenticationFilter
Basic HTTP身份验证拦截器,主要属性: applicationName:弹出登录框显示的信息(application);
logout
org.apache.shiro.web.filter.authc
.LogoutFilter
退出拦截器,主要属性:redirectUrl:退出成功后重定向的地址(/);示例“/logout=logout”
user
org.apache.shiro.web.filter.authc
.UserFilter
用户拦截器,用户已经身份验证/记住我登录的都可;示例“/**=user”
anon
org.apache.shiro.web.filter.authc
.AnonymousFilter
匿名拦截器,即不需要登录即可访问;一般用于静态资源过滤;示例“/static/**=anon”
授权相关的
roles
org.apache.shiro.web.filter.authz
.RolesAuthorizationFilter
角色授权拦截器,验证用户是否拥有所有角色;主要属性: loginUrl:登录页面地址(/login.jsp);unauthorizedUrl:未授权后重定向的地址;示例“/admin/**=roles[admin]”
perms
org.apache.shiro.web.filter.authz
.PermissionsAuthorizationFilter
权限授权拦截器,验证用户是否拥有所有权限;属性和roles一样;示例“/user/**=perms["user:create"]”
port
org.apache.shiro.web.filter.authz
.PortFilter
端口拦截器,主要属性:port(80):可以通过的端口;示例“/test= port[80]”,如果用户访问该页面是非80,将自动将请求端口改为80并重定向到该80端口,其他路径/参数等都一样
rest
org.apache.shiro.web.filter.authz
.HttpMethodPermissionFilter
rest风格拦截器,自动根据请求方法构建权限字符串(GET=read, POST=create,PUT=update,DELETE=delete,HEAD=read,TRACE=read,OPTIONS=read, MKCOL=create)构建权限字符串;示例“/users=rest[user]”,会自动拼出“user:read,user:create,user:update,user:delete”权限字符串进行权限匹配(所有都得匹配,isPermittedAll);
ssl
org.apache.shiro.web.filter.authz
.SslFilter
SSL拦截器,只有请求协议是https才能通过;否则自动跳转会https端口(443);其他和port拦截器一样;
其他
noSessionCreation
org.apache.shiro.web.filter.session
.NoSessionCreationFilter
不创建会话拦截器,调用 subject.getSession(false)不会有什么问题,但是如果 subject.getSession(true)将抛出 DisabledSessionException异常;
- Hello Mr.J——shiro+springmvc
- Hello Mr.J——shiro+cas
- Hello Mr.J——shiro 简单地认识
- Hello Mr.J——shiro 实现session共享
- Hello Mr.J——shiro session管理机制
- Hello Mr.J——tomcat优化
- Hello Mr.J——SQL分类
- Hello Mr.J——开个新坑,Docker!
- Hello Mr.J——Docker
- Hello Mr.J——Docker
- Hello Mr.J——Docker
- Hello Mr.J——Docker
- Hello Mr.J——J2EE规范自用总结
- Hello Mr.J——SSH简单自用向总结
- Hello Mr.J——用CXF框架发布WebService
- Hello Mr.J——WebService传递Map类型
- Hello Mr.J——What do you want?Mycat~
- Hello Mr.J——Docker 安装和启动
- 1112: [POI2008]砖块Klo
- 重装win10关机不断电的解决办法
- 最佳循环&&嵌套&&数组
- Hadoop新手学习指导
- jmeter中不用time函数,用Random函数设置正确的时间格式
- Hello Mr.J——shiro+springmvc
- androidEdittext控件使用
- Ajax慕课网
- 平面的投影变换(3)——利用无穷线进行仿射校正
- php list
- 导出csv文件时,处理分隔符问题
- C++ Prime Plus 学习有感
- 阿里云App性能优化实践
- 设计模式----备忘录模式