springmvc没有将静态文件排除掉。要排除一下,那么security怎么排除的来着。
anon不需要认证,authc需要认证
https://github.com/zchengi/shiro-learn
loginUrl 登录时访问的页面,即没有登录时访问任何【页面】跳转的页面。默认的是webroot下的login.jsp页面
unauthorizedUrl 认证未通过访问的页面,即经过认证但是没有相应的权限时跳转的页面
@requestMapping produces 属性中可以设置返回的编码格式
Shiro 自带拦截器
controller类中的写法
spring.xml中进行shiro配置
用户登录界面: subLogin(User user)
produces="application/json;charset=utf-8"
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"的形式即可。
在web.xml中配置shiro的过滤器
shiro和spring集成所需的jar包
源码位置:https://gitee.com/ljl17625/shiro-spring/tree/master
spring整合shiro
1.创建项目
2.导入依赖(spring、shiro、springmvc、spring-shiro、shiro-web)
3.创建web.xml(配置前端控制器、过滤器、加载初始化springmvc.xml、加载spring.xml、post请求乱码)
4.创建springmvc.xml(配置上下文扫描,配置mvc的处理器适配器、处理器适配器,配置静态文件扫描)
5.创建spring.xml(加到容器中:shiroFilter,创建SecurityMananger对象并设置自定义Realm,定义自定义的Realm,设置加密的算法)
6.创建login.html (表单账号、密码提交到UserController)
7.自定义Realm
8.创建UserController(接受前台数据,进行shiro登录验证,如果成功返回成功,打印错误)
Spring+shiro
源码https://gitee.com/ljl17625/shiro-spring/tree/master
项目结构图

springmvc.xml的配置
<?<?xml version="1.0" encoding="UTF-8"?>
spring.xml的配置
<?<?xml version="1.0" encoding="UTF-8"?>
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>