项目结构
![这里写图片描述](https://img-blog.csdn.net/20170711093009670?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYnBxZHdv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
web.XML配置
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring.xml;
classpath*:config/spring-mybatis.xml
</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>springmvc.root</param-value>
</context-param>
<filter>
<filter-name>SpringEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SpringEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>log4jConfigLocation</param-name>
<param-value>classpath:config/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>6000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/errorpage/404.jsp</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/WEB-INF/errorpage/405.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/errorpage/500.jsp</location>
</error-page>
</web-app>
spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="config/spring-mybatis.xml"/>
<import resource="config/shiro-context.xml"/>
<context:property-placeholder location="classpath:config/jdbc.properties" />
<context:component-scan base-package="com.deng.serviceImp" />
</beans>
spring-mvc配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" 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-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.deng.controller" />
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp" />
</beans>
mybatis-config配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases >
</typeAliases>
<mappers>
</mappers>
</configuration>
spring-mybatis配置
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
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-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName">
<value>${jdbc_driverClassName}</value>
</property>
<property name="url">
<value>${jdbc_url}</value>
</property>
<property name="username">
<value>${jdbc_username}</value>
</property>
<property name="password">
<value>${jdbc_password}</value>
</property>
<property name="maxActive">
<value>20</value>
</property>
<property name="initialSize">
<value>1</value>
</property>
<property name="maxWait">
<value>60000</value>
</property>
<property name="maxIdle">
<value>20</value>
</property>
<property name="minIdle">
<value>3</value>
</property>
<property name="removeAbandoned">
<value>true</value>
</property>
<property name="removeAbandonedTimeout">
<value>180</value>
</property>
<property name="connectionProperties">
<value>clientEncoding=UTF-8</value>
</property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource" p:configLocation="classpath:config/mybatis-config.xml"
p:mapperLocations="classpath:com/deng/mapper/*.xml" />
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="com.deng.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory">
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
</beans>
shiro-context配置
<?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.jsp" />
<property name="successUrl" value="/home" />
<property name="unauthorizedUrl" value="/403.do" />
<property name="filterChainDefinitions">
<value>
/login = anon
/home = authc, perms[/home]
/** = authc
</value>
</property>
</bean>
<bean id="myShiroRealm" class="com.deng.shiro.myReam">
</bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myShiroRealm"></property>
</bean>
</beans>
shiro验证登录验证类
package com.deng.shiro
import java.util.List
import org.apache.shiro.authc.AuthenticationException
import org.apache.shiro.authc.AuthenticationInfo
import org.apache.shiro.authc.AuthenticationToken
import org.apache.shiro.authc.IncorrectCredentialsException
import org.apache.shiro.authc.SimpleAuthenticationInfo
import org.apache.shiro.authc.UnknownAccountException
import org.apache.shiro.authc.UsernamePasswordToken
import org.apache.shiro.authz.AuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo
import org.apache.shiro.realm.AuthorizingRealm
import org.apache.shiro.subject.PrincipalCollection
import org.springframework.beans.factory.annotation.Autowired
import com.deng.entity.User
import com.deng.entity.UserRole
import com.deng.service.userService
public class myReam extends AuthorizingRealm{
@Autowired
private userService ser
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pri) {
// TODO Auto-generated method stub
System.out.println("this is 权限~~~~~~~")
User user=(User) pri.getPrimaryPrincipal()
Integer id = user.getId()
if(id==1){
System.out.println("this is id====1")
List<UserRole> qRole = ser.queryUserRole(id)
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo()
//获取能够访问的URL列表
List<String> qu = ser.queryPermission(qRole.get(0).getRoleId())
for(String r:qu){
System.out.println("r==="+r)
info.addStringPermission(r)
}
return info
}
return null
}
//登录
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token1) throws AuthenticationException {
// TODO Auto-generated method stub
System.out.println("this is 登录shiro 验证·~~~~~~~~~~~~" )
UsernamePasswordToken token=(UsernamePasswordToken)token1
String username = token.getUsername()
char[] password = token.getPassword()
String pass=new String(password)
System.out.println(username)
System.out.println(pass)
System.out.println(22222222)
if(username!=null){
User user = ser.queryUserByUsername(username)
System.out.println("user====="+user)
//账号不存在
if(user == null) {
System.out.println("1账号或密码不正确")
throw new UnknownAccountException("账号或密码不正确")
}
if(user!=null){
//密码错误
if(!pass.equals(user.getPassword())) {
System.out.println("2账号或密码不正确")
throw new IncorrectCredentialsException("账号或密码不正确")
}
System.out.println(user)
return new SimpleAuthenticationInfo(user, user.getPassword(),getName())
}
}
return null
}
}
logincontroller类
package com.deng.controller
import javax.annotation.Resource
import javax.servlet.http.HttpServletRequest
import org.apache.shiro.SecurityUtils
import org.apache.shiro.authc.AuthenticationException
import org.apache.shiro.authc.UnknownAccountException
import org.apache.shiro.authc.UsernamePasswordToken
import org.apache.shiro.subject.Subject
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import com.deng.entity.User
import com.deng.service.userService
@Controller
public class LoginController {
@Autowired
private userService ser
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String Login(String username, String password,HttpServletRequest req) {
try {
UsernamePasswordToken token = new UsernamePasswordToken(username,password)
Subject subject = SecurityUtils.getSubject()
subject.login(token)
} catch (UnknownAccountException e) {
e.printStackTrace()
req.getSession().setAttribute("errorMessage", "用户名或密码错误")
return "login"
} catch (AuthenticationException e) {
e.printStackTrace()
req.getSession().setAttribute("errorMessage", "用户名或密码错误")
return "login"
}
// 登录后存放进shiro token
return "home"
}
}
login.jsp
<form action="login.do" method="post">
username:<input type="text" name="username"><p>
password:<input type="password" name="password">
<input type="submit">
<p>
<span>${sessionScope.errorMessage}</span>
</form>
密码错误
![这里写图片描述](https://img-blog.csdn.net/20170711094346409?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYnBxZHdv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
登录成功
![这里写图片描述](https://img-blog.csdn.net/20170711094406293?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYnBxZHdv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)