文章目录
- Shiro集成Spring
- 1.1 创建新module(maven-archetype-webapp类型):shiro-Spring
- 1.2 在shiro-web中引入spring的包:spring-context、spring-webmvc、spring-core、spring-web、spring-jdbc;shiro包:shiro-core、shiro-spring、shiro-web;myslq:mysql-connector-java;druid;junit
- 1.3 在web.WEB-INF文件中创建web.xml文件,在web.xml文件中配置spring;springmvc;shiro
- 1.4 在main.resources中创建spring文件夹,存放配置文件spring.xml、springmvc.xml
- 1.5 创建login.html、403.html、welcome.html
- 1.6 自定义Realm:CustomRealm
- 1.7 创建User接收认证信息
- 1.8 构建Controller:UserController来登陆认证
Shiro集成Spring
1.1 创建新module(maven-archetype-webapp类型):shiro-Spring
1.2 在shiro-web中引入spring的包:spring-context、spring-webmvc、spring-core、spring-web、spring-jdbc;shiro包:shiro-core、shiro-spring、shiro-web;myslq:mysql-connector-java;druid;junit
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
</dependencies>
1.3 在web.WEB-INF文件中创建web.xml文件,在web.xml文件中配置spring;springmvc;shiro
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>shiro-spring</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--配置加载spring核心文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring.xml</param-value>
</context-param>
<!--配置spring监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置spring编码过虑器-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置springmvc核心控制器-->
<servlet>
<servlet-name>springmvc</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>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置shiro过虑器-->
<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>
</web-app>
1.4 在main.resources中创建spring文件夹,存放配置文件spring.xml、springmvc.xml
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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<context:component-scan base-package="com.evancjj.shiro"/>
<bean class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" id="shiroFilter">
<property name="securityManager" ref="securityManager"/>
<!--登陆认证页面-->
<property name="loginUrl" value="login.html"/>
<!--未认证跳转页面-->
<property name="unauthorizedUrl" value="403.html"/>
<!--欢迎页面-->
<property name="successUrl" value="welcome.html"/>
<!--过滤器链-->
<property name="filterChainDefinitions">
<value>
<!--过滤器链从上往下加载-->
<!--不需要认证-->
/login.html=anon
/subLogin=anon
<!--经过认证之后才可以访问相应路径-->
/*=authc
</value>
</property>
</bean>
<!--创建SecurityManager环境-->
<bean class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" id="securityManager">
<property name="realm" ref="customRealm"/>
</bean>
<!--创建自定义Realm对象设置到创建SecurityManager环境当中-->
<bean class="com.evancjj.shiro.realm.CustomRealm" id="customRealm">
<property name="credentialsMatcher" ref="credentialsMatcher"/>
</bean>
<!--shiro加密-->
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher" id="credentialsMatcher">
<!--加密算法-->
<property name="hashAlgorithmName" value="md5"/>
<!--加密次数-->
<property name="hashIterations" value="1"/>
</bean>
</beans>
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<!--扫面controller-->
<context:component-scan base-package="com.evancjj.shiro.controller"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
</beans>
1.5 创建login.html、403.html、welcome.html
login.html:认证登陆页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="/subLogin" method="post">
用户名:<input type="text" name="username"><br>
密 码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
403.html:认证失败页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>403</title>
</head>
<body>
无权限
</body>
</html>
welcome.html:欢迎页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>啦啦</title>
</head>
<body>
啦啦
</body>
</html>
1.6 自定义Realm:CustomRealm
package com.evancjj.shiro.realm;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class CustomRealm extends AuthorizingRealm {
/**
* 重写授权方法
*
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String username = (String) principalCollection.getPrimaryPrincipal();
Set<String> roleSet = getRoleByUsername(username);
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addRoles(roleSet);
return info;
}
/**
* 模拟从数据库获取角色信息
*
* @param username
* @return
*/
private Set<String> getRoleByUsername(String username) {
Set<String> roleSet = new HashSet<>();
roleSet.add("admin");
roleSet.add("user");
return roleSet;
}
/**
* 重写认证方法
*
* @param authenticationToken
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
String username = (String) authenticationToken.getPrincipal();
String password = getPasswordByUsername(username);
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, password, this.getName());
//盐值加密
// info.setCredentialsSalt(ByteSource.Util.bytes(username));
return info;
}
/**
* 模拟从数据库获取密码
*
* @param username
* @return
*/
private String getPasswordByUsername(String username) {
Map<String, String> userMap = new HashMap<>();
userMap.put("cjj", "e10adc3949ba59abbe56e057f20f883e");
return userMap.get(username);
}
public static void main(String[] args) {
Md5Hash hash = new Md5Hash("123456");
System.out.println(hash);
}
}
1.7 创建User接收认证信息
package com.evancjj.shiro.pojo;
public class User {
private String username;
private String password;
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
1.8 构建Controller:UserController来登陆认证
package com.evancjj.shiro.controller;
import com.evancjj.shiro.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/user/")
public class UserController {
@RequestMapping(value = "/subLogin", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
@ResponseBody
public String login(User user) {
// 获取主体
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
try {
// 登陆认证
subject.login(token);
} catch (AuthenticationException e) {
return e.getMessage();
}
return "登录成功";
}
}