您的位置:首页 > 编程语言 > Java开发

shiro学习:shiro整合SpringMVC的web项目

2017-10-22 14:48 507 查看
这篇文章只是介绍了shiro与springweb项目进行的整合,并没有涉及到认证的实现,如果您需要认证的相关实现,请看下我的下一篇:Shiro实现登录和退出

一、准备环境

与其它java开源框架类似,将shiro的jar包加入项目就可以使用shiro提供的功能了。shiro-core是核心包必须选用,还提供了与web整合的shiro-web、与spring整合的shiro-spring、与任务调度quartz整合的shiro-quartz等,下边是shiro各jar包的maven坐标。

<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-quartz</artifactId>
<version>1.2.3</version>
</dependency>
二、配置文件
2.1web.xml添加shiro Filter

建议将filter-mapping放在其他filter-mapping的上边,便于第一次执行shiro的filter

<!-- shiro过虑器,DelegatingFilterProxy通过代理模式将spring容器中的bean和filter关联起来 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<!-- 设置true由servlet容器控制filter的生命周期 -->
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<!-- 设置spring容器filter的bean id,如果不设置则找与filter-name一致的bean-->
<init-param>
<param-name>targetBeanName</param-name>
<param-value>shiroFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>



2.2配置application-shiro.xml

注意:自动realm的路径一定要用我们自己创建的realm路径

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<!-- web.xml中shiro的filter需要的bean -->
<!-- Shiro 的Web过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<!-- loginUrl认证提交地址,如果没有认证将会请求此地址进行认证,请求此地址将由formAuthenticationFilter进行表单认证 -->
<property name="loginUrl" value="/login.do" />
<!-- 认证成功同意跳转到first.action 建议不要配置,shiro自动到上一个请求路径 -->
<property name="successUrl" value="/index.do" />
<!-- 通过unauthorizedUrl指定没有权限操作时的权限页面 -->
<property name="unauthorizedUrl" value="/refuse.jsp" />

<!-- 过虑器链定义,从上向下顺序执行,一般将/**放在最下边 -->
<property name="filterChainDefinitions">
<value>
<!-- 对静态资源设置匿名访问 -->
/images/** = anon
/js/** = anon
/styles/** = anon

<!--/** = authc 所有的url都授权后才能访问  -->
/** = authc

</value>
</property>
</bean>
<!-- SecurityManager -->
<!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 注入自定义realm -->
<property name="realm" ref="customRealm" />
</bean>

<!-- 自定义 realm -->
<bean id="customRealm" class="org.lpl.controller.shiro.CustomRealm">
</bean>

</beans>

2.3自定义realm

public class CustomRealm extends AuthorizingRealm {

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// TODO Auto-generated method stub
return null;
}

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
System.out.println("shiror认证");
return null;
}

}
2.4登录controller

因为我们在shiro的配置文件中配置了如果没有认证就会跳到login.do这个controller,通过这个controller我们就将将没有认证的让他去登录页面

@Controller
public class LoginCotroller {
/**
* 此方法只处理登录失败,如果登录成功shiro会自动跳转到上一个请求路径
* @param request
* @return
* @throws Exception
*/
@RequestMapping("login.do")//这个路径要和shiro.xml配置的路径要一样
public String login(HttpServletRequest request) throws Exception{

return "login";
}
}


三、测试

直接访问我们的项目路径(例如:localhost:8080/SSMShiro),如果自己跳到登录页面说明成功



四、我的目录结构:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: