-
web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 注册spring提供的针对POST请求的中文乱码问题 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
查看全部 -
spring.xml的配置
<?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="login.html"/>
<property name="unauthorizedUrl" value="403.html"/>
<property name="filterChainDefinitions">
<value>
/login.html = anon
/subLogin = anon
/* = authc
</value>
</property>
</bean>
<!--创建SecurityMananger对象-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--设置自定义Realm-->
<property name="realm" ref="realm"/>
</bean>
<!--定义自定义的Realm-->
<bean id="realm" class="com.imooc.shiro.realm.CustomerRealm">
<property name="credentialsMatcher" ref="credentialsMatcher"/>
</bean>
<!--设置加密的算法-->
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"
id="credentialsMatcher">
<property name="hashAlgorithmName" value="md5"/>
<property name="hashIterations" value="1"/>
</bean>
</beans>查看全部 -
Shiro的整体架构:
shiro通过Secunrity Manager提供安全服务
Secunrity Manager管理着其他组件的实例
1、Authenticator(认证器):管理登录、登出。
2、Authorizer (授权器):赋予主体权限。
3、Session Manager(Session管理器):Shiro'自己实现的管理机制,不借用任何容器使用Session。
4、Session Dao(提供Session的操作): 主要有:增、删、改、查。
5、Cache Manager(缓存管理器):角色和权限数据缓存。
6、Pauggable Rwalms(数据库和数据源的桥梁):shiro获取数据是通过rwalms来获取。
流程:
1、主体提交请求到Secunrity Manager。
2、Secunrity Manager调用Authenticator进行认证。(Authenticator认证获取数据是通过rwalms获取的,再从数据源中获取信息)数据源信息和主体提交的信息作比对。
3、(Authorizer授权获取数据是通过rwalms获取的,再从数据源中获取信息)数据源信息和主体提交的信息作比对。
4、数据加密
查看全部 -
Shiro过滤器
内建过滤器:roles[a,b,c] 必须权限都符合才可以、perms[a,b,c]必须权限同时具备才可以。
anon 代表无需权限
authc 代表需要认证才能访问
user 代表需要存在用户对象才能被访问
logout 登录退出才能被访问
perms 拥有权限才能被访问
roles 拥有角色才能被访问
port 相应端口号才能访问
自定义,分认证与授权,如果是认证则继承认证的类,如果是授权则继承授权的类。然后重写方法,其中参数Object o 即代表存在哪些参数,即perms[a,b,c]中的abc然后判断返回结果true 或者false即可。
在后台xml文件中引入该filter的bean,然后在主过滤器中引入filter的标签,写入entry key 与 value-ref,然后在filter过滤中
filterChainDefinitions地方按照之前的规则使用该自定义过滤,写入
key值即可。
查看全部 -
Shiro集成Spring,步骤以及注意事项如下:
创建Maven的Web项目,创建完毕之后,按照Maven的规范创建出相应的Java源码文件以及相应的test测试目录。并且将web-info下的文件删除。
设置java文件夹以及resources文件夹分别为源码类型以及资源文件类型,具体可以点击文件夹右键,找到Mark Dirctory as选择。
在resources中创建spring文件夹,并且创建spring.xml以及spring-mvc.xml文件。
在web-info中创建web.xml文件。一般的idea不可创建出web.xml文件,所以此时按照网上给的方法,直接选择菜单中的File---Project Stru----Facts 第一个空白点击处,添加web.xml文件。点击确定即可。路径要写对,是在web-info下。此种方法如果不行,则需要手动写入web.xml文件。
web.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/spring.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
其次在Maven的pom.xml文件中引入相应的spring、springmvc 、shiro 、shiro-spring、shiro-web的jar包,注意必须版本保持匹配,否则启动容易报404错误,我就是因为这个问题。如下是pom.xml文件引入的包:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- 2)SpringWeb Dependency --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- 3)Shiro Dependency --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version> </dependency> <!-- 4)ShiroWeb Dependency --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.4.0</version> </dependency> <!-- 5)ShiroSpring Dependency --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</version> </dependency>
在相应的resources资源文件下建立spring文件夹,在spring-mvc.xml文件中写入扫描文件以及驱动、过滤的标签,如下:
<context:component-scan base-package="com.imooc.controller"/> <mvc:annotation-driven /> <mvc:resources mapping="/*" location="/"/>
在spring.xml文件中,写入相应的自定义的Realm的bean标签、加密的Hashed的bean标签(注入到Realm中)、写入默认的web的权限管理的bean标签(注入Realm)、写入ShiroFilterFactoryBean的bean标签(将web的权限管理注入进去)即可。
此外ShiroFilter的bean标签里面,还可以规定loginurl的值,即登录的页面,unauthor..登录失败没权限访问的页面,filterChainDefinitions过滤链,(过滤链里面可以以a=b的形式进行过滤,例:/login.html = anon 表示在访问login.html页面的时候不需要任何权限,anon表示无需权限,authc代表必须认证通过才能访问,一般链是从上往下匹配,只要匹配的相应结果,则直接返回。所以一般把/*=authc放在最后。)下面是源码:
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="login.html"/> <property name="unauthorizedUrl" value="403.html"/> <property name="filterChainDefinitions"> <value> /login.html = anon /subLogin = anon /* = authc </value> </property> </bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="customRealm"/> </bean> <bean id="customRealm" class="com.imooc.realm.CustomRealm"> <property name="credentialsMatcher" ref="hashedCredentialsMatcher"/> </bean> <bean id="hashedCredentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher"> <property name="hashAlgorithmName" value="md5"/> <property name="hashIterations" value="1"/> </bean>
之后基本上就是写入controller以及html网页即可。之后启动tomcat自动访问,其中关于编码的问题需要在@RequestMapping中设置produces="application/json;charset=utf-8"的形式即可。
查看全部 -
自定义的Realm,主要注意一下几个步骤:
首先继承并实现类AuthorizingRealm的方法。其中方法
doGetAuthenticationInfo 主要做认证操作,即可以通过获取其中的
用户名,查询出相应的密码,然后将用户名与密码一并返回,shiro会自动根据传入的用户名与密码与此Realm返回的用户名和密码作比对,返回你想要的结果。同理doGetAuthorizationInfo主要是用来做角色与权限的验证,也是通过用户名,从数据库中查找到相应的角色或者权限的数据并返回一个Simple的授权类,授权系统会根据传入的参数与返回的结果集对比,存在返回true不存在则抛异常。
两者方法只能获取用户名称,通过用户名称连接数据库获取其他信息。这里只是做数据准备操作,并不做判断是否传入的值与其相符的操作,此操作在此之后进行。
根据两者的返回对象分别返回Simple类型的对象。认证的对象需要将你获取的用户名和密码放到构造方法中。授权的对象需要你set到相应的方法中。
例子中只是从本地写死静态类,之后必须写入相应的数据库连接。
查看全部 -
源码位置:https://gitee.com/ljl17625/shiro-spring/tree/master
查看全部 -
源码https://gitee.com/ljl17625/shiro-spring/tree/master
查看全部 -
springmvc.xml的配置
<?
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.imooc.controller"/>
<mvc:annotation-driven/>
<!--排除静态文件-->
<mvc:resources mapping="/*" location="/"/>
</beans>查看全部 -
1 Authenticator 授权验证,例如登录登出
2 Authorizer 用于用户的角色管理,给用户角色赋予什么权限
3 Session Manager 回话管理
4 Cache Mananger 可以将用户角色信息缓存起来
5 Session DAO 用来对session的增删改查
6 Realms 用来管理数据
查看全部 -
Shiro自动登录,意为记住密码。将用户名与密码记录在Cookie中。
主要通过配置来实现自动登录。步骤:
在Spring.xml文件中进行配置,配置CookieRememberMeManager,并且利用属性cookie,将外边的bean的SimpleCookie注入进去。SimpleCookie用构造方法设置名称,之后通过属性用maxAge设置超时时间即可。
在登录的时候,传入UserNameAndPassowrdToken的时候,其对象可以设置setRememberMe的属性,为true即为记住登录。
查看全部 -
Shiro加密
通过HashedCredentialsMaster的方式加密,创建对象,并设置加密次数以及加密名称,将此加密对象设置到Realm的setCredentialsMaster的方法中,即该Realm采用此密码加密的方式。
后台的密码采用MD5的Hash码值来存储。如果想将密码转成MD5Hash码可以采用MD5Hash对象,在构造方法中写入密码,
打印输出其对象的toString方法即时期hash码值。
加盐,实际为在密码中加入相应的其他名称拼接在一起,组成一个新的密码。如果想存储加盐之后的Hash码,仍旧采用MD5Hash对象,在构造方法的第二个参数中加入盐的名称,同样打印toString方法即可显示出来。其次在自定义的Realm认证方法中,返回Simple之前,设置Simple的加盐名称,即采用setCredentailSalt方法写入盐的名称即可。
以上两种加密方式,对Realm改动较小,除非加盐才改动。
在自定义的Realm加盐的时候,需要将字符值转成相应的ByteSource,则需要用ByteSource.Util.bytes(字符)即可,将此值设置到Simple的返回值中。
查看全部 -
Shiro安全框架:简单灵活、可脱离Spring 独立存在、粒度较粗
Spring Security :复杂笨重、必须依托与Spring、粒度较细
由于权限管理模块一般都是在资源一层,如果更深一层的话,则可能会与业务代码进行耦合,故提倡Shiro做权限管理,Spring官网也是用Shiro进行的权限管理。
查看全部 -
Shiro缓存管理
其缓存管理仍旧是依赖于Redis进行实现。
意为将从数据库取出来的数据放置到Redis缓存中去,如果缓存中有直接取出,没有则去数据库中获取并且放置到Reids缓存中。
步骤:
与之前的Session管理一致。首先创建CacheManager并且实现CacheManager接口。
创建Cache类实现Cache接口,其中有增删改查等方法。同样通过序列化与反序列化、二进制数组键值对等实现其中方法。
在CacheManager类中注入Cache类,并且将其作为返回值写入到实现的唯一方法的返回处。
将CacheManager在Spring.xml中进行配置Bean,并且将次设置到SecurityManager中。
都是通过JedisUtil完成的操作,均是将数据保存到了Redis中,无论是Session还是缓存只是多了一个Redis进行管理。
查看全部 -
Shiro的IniRealm 形式:此形式主要是将数据存放到相应的user.ini即文件系统中,通过给定的格式,从文件中查找相应的数据是否存在。
步骤:
1.删除之前SimpleAccountRealm,在实体中创建IniRealm并实例化。在构造方法中传入相应的user.ini地址。此文件地址一般写为:classpath:user.ini的形式。
需要在相应的包中建立resource文件夹,并且放入user.ini文件。
2.user.ini文件的定义格式为:[users] 用户名=密码,角色 [roles] 角色=权限名(例子:amdin=user:delete,user:update)
3.与之前认证与授权步骤一样,直接将IniRealm放入到SecurityManager中,进行登录认证,之后进行checkRoles验证与checkPersimmon验证权限等操作即可。
查看全部
举报