我们这个项目主要就是做一个商城,能够普通购物和秒杀功能,然后登录计划要实现单点登录的,但是难度有点大,于是做了下权限登录,这样不同角色权限不同,能看到的东西也不一样。而且shiro可以做成单点登录,所以还是考虑实现单点登录。这里讲的主要是shiro怎么整合到ssm框架中,尽量讲的详细,因为我看了别人的博客都踩了不少坑啊
1.项目目录结构
2.Maven引入shiro的jar包
<!-- shiro -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</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-web</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.2.3</version>
</dependency>
3.web.xml的配置,spring的过滤器,这里的shiroFilter要跟配置文件的一致,不然会报错,找不到
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<!-- spring整合安全框架 -->
<servlet>
<servlet-name>seckill-dispatchServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置springmvc的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml, classpath:spring/shiro-context.xml
</param-value>
</init-param>
<load-on-startup> 1 </load-on-startup>
</servlet>
<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-mapping>
<servlet-name>seckill-dispatchServlet</servlet-name>
<!--直接拦截所有请求,不再采用spring2.0的/*或者*.do方式-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.在applicationContext-dao.xml中引入,然后spring下新建配置文件,如下
<import resource="shiro-context.xml" />
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" /> <!--加载管理器-->
<property name="loginUrl" value="/login" /> <!--没有登录的时候,跳转到这个页面,也就是requestMapping的映射路径-->
<property name="unauthorizedUrl" value="/nopermission" /> <!--当没有权限的时候,跳转到这个url-->
<property name="filterChainDefinitions">
<value>
/login = anon <!--可以不需要登录-->
/readName = authc, perms[/readName] <!-- perms 表示需要该权限才能访问的页面 -->
/readData = authc, perms[/readData]
/* = authc <!-- authc 表示需要认证才能访问的页面 -->
</value>
</property>
</bean>
<!-- 自定义Realm -->
<bean id="myShiroRealm" class="com.hui.login.MyShiroReaml">
<!-- businessManager 用来实现用户名密码的查询 -->
<property name="userService" ref="accountService"/>
</bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 注入realm -->
<property name="realm" ref="myShiroRealm"/>
</bean>
<!--声明一个Service 注入到自定义Realm-->
<bean id="accountService" class="com.hui.service.UserServiceImpl"/>
</beans>
5.实现权限登录还要写dao,service,controller层等,我在这里就不细说了,具体可以看源码,这里主要看一下怎么实现权限登录的
https://www.cnblogs.com/fengli9998/p/6676783.html这篇博客讲的挺细的。shiro就是一个权限登录开源框架,而且还可以对数据进行加密保证其安全性如md5和sha,而且不仅可以用在web,还可以用于c/s和分布式中
一些概念:
subject:主体,可以是用户也可以是程序,主体要访问系统,系统需要对主体进行认证、授权。
securityManager:安全管理器,主体进行认证和授权都 是通过securityManager进行。它包含下面的认证器和授权器。
authenticator:认证器,主体进行认证最终通过authenticator进行的。
authorizer:授权器,主体进行授权最终通过authorizer进行的。
sessionManager:web应用中一般是用web容器对session进行管理,shiro也提供一套session管理的方式。可以实现单点登录。
SessionDao: 通过SessionDao管理session数据,针对个性化的session数据存储需要使用sessionDao。
cache Manager:缓存管理器,主要对session和授权数据进行缓存,比如将授权数据通过cacheManager进行缓存管理,和ehcache整合对缓存数据进行管理。
realm:域,领域,相当于数据源,通过realm存取认证、授权相关数据。(它的主要目的是与数据库打交道,查询数据库中的认证的信息(比如用户名和密码),查询授权的信息(比如权限的code等,所以这里可以理解为调用数据库查询一系列的信息,一般情况下在项目中采用自定义的realm,因为不同的业务需求不一样))
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
System.out.println("1:"+token.getUsername());
User user = userService.getUserByUserName(token.getUsername());
System.out.println("2");
if(user==null){
return null; }
AuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), this.getClass().getSimpleName());
return info; }
认证原理:
1、通过配置文件shiro-context.xml创建securityManager
2、AuthorizationInfo用于聚合授权信息的,提交用户名和密码,返回授权信息
3、securityManager进行认证,securityManager最终由ModularRealmAuthenticator进行认证。
4、ModularRealmAuthenticator调用IniRealm(给realm传入token) 去配置文件中查询用户信息
@RequestMapping(value = "/login")
public String Login(String username, String password, HttpSession session, Model model){
if(username==null){
model.addAttribute("message", "查看页面");
return "login";
}
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token=new UsernamePasswordToken(username,password);
User user;
try {
subject.login(token);
user = (User)subject.getPrincipal();
session.setAttribute("user",subject);
model.addAttribute("message", "信息");
} catch (UnknownAccountException e) {
model.addAttribute("message", "查看信息");
return "index";
}
return "test";
}
写到后面脖子不行了,改天再写,好好学习一下权限登录55~
源码https://github.com/HannahLihui/Shop









网友评论