注:由于是学习阶段搭建的项目,命名和代码很多不规范
- 基本准备
创建maven的父项目
注意:父项目为maven project,直接创建一个简单工程
将ssm项目中的模块抽出分别创建maven的子项目
在父项目的基础上创建maven module
dao层:
entity层:
interface层(service层):
创建为jar项目
serviceImpl层:
web层:
以上为war包的方式创建项目
注意:若项目报错,请手动在webapp下的WEB-INF下添加web.xml文件
-
配置文件
父项目引入jar包依赖
注意:
- dubbo的jar包与springframework存在冲突,jar包依赖导入时解决
- shiro的缓存技术和ehcache存在冲突,引入低版本的ehcache的依赖,在pom中已解决
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- dubbo的核心包,去除掉springframework包之间的冲突 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- 在运行时shiro缓存与ehcache-core存在冲突,所以选用2.4 8的jar包 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.4.8</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <!-- JSTL标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 日志文件管理包 --> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- 格式化对象,方便输出日志 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> <!-- 映入JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- 上传组件包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <!-- shiro --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> <!-- zookeeper的依赖包 --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> </dependencies>
maven父项目下载完所有jar包之后进行更新
dao层:
dao层与mybatis的整合,可以使用逆向工程进行创建,这里不使用逆向工程,自己编写mapper与xml
配置文件:
注意:若dubbo标签报错,请手动引入xsd
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动扫描 <context:component-scan base-package="com.cn.uuu" /> -->
<!-- dubbo相关配置 -->
<dubbo:application name="demo-serviceImpl" />
<dubbo:registry address="zookeeper://47.100.126.191:2181"></dubbo:registry>
<dubbo:annotation package="cn.xuxiaonan.serviceimpl" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${jdbc.initialSize}" />
<!-- 连接池最大数量 -->
<property name="maxActive" value="${jdbc.maxActive}" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${jdbc.maxIdle}" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${jdbc.minIdle}" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${jdbc.maxWait}" />
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapping/*.xml" />
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.xuxiaonan.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
jdbc配置:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:/xuxiaonan?useUnicode=true&characterEncoding=UTF-8
jdbc.username=
jdbc.password=
jdbc.initialSize=0
jdbc.maxActive=20
jdbc.maxIdle=20
jdbc.minIdle=1
jdbc.maxWait=60000
日志配置:
#\u5B9A\u4E49LOG\u8F93\u51FA\u7EA7\u522B
#log4j.rootLogger=INFO,Console,File
#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0
#log4j.appender.Console=org.apache.log4j.ConsoleAppender
#log4j.appender.Console.Target=System.out
#\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F
#log4j.appender.Console.layout = org.apache.log4j.PatternLayout
#log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6
#log4j.appender.File = org.apache.log4j.RollingFileAppender
#\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55
#log4j.appender.File.File = logs/ssm.log
#\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F
#log4j.appender.File.MaxFileSize = 10MB
# \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7
#log4j.appender.File.Threshold = ALL
#log4j.appender.File.layout = org.apache.log4j.PatternLayout
#log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
log4j.rootLogger=Debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Print the date in ISO 8601 format
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN
serviceImpl层:
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" metadata-complete="true">
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-dao.xml</param-value>
</context-param>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
web层:
注意:这里在整合shiro的时候,由于要配置过滤器的原因,我将shiro整合在了web层。然后应该公共抽取还是整合在web层中这一点我还不会。
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<!-- 配置注解的映射器和适配器 -->
<mvc:annotation-driven ></mvc:annotation-driven>
<!-- 引用dubbo服务 -->
<!-- dubbo相关配置 -->
<dubbo:application name="demo-web" />
<dubbo:registry address="zookeeper://47.100.126.191:2181"></dubbo:registry>
<dubbo:annotation package="cn.xuxiaonan.controller" />
<!--使用配置注入dubbo的服务-->
<dubbo:reference id="userService" interface="cn.xuxiaonan.service.userService"/>
<!-- 开启shiro的注解支持 -->
<bean id="defaultAdvisorAutoProxyCreator"
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<!-- 必须改为true,即使用cglib方式为Action创建代理对象。默认值为false,使用JDK创建代理对象,会造成问题 -->
<property name="proxyTargetClass" value="true"></property>
</bean>
<!-- 使用shiro框架提供的切面类,用于创建代理对象 -->
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"></bean>
<!-- 静态资源处理 -->
<mvc:default-servlet-handler/>
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">test/error</prop>
</props>
</property>
</bean>
<!-- 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="/user/login" />
<property name="unauthorizedUrl" value="/error" />
<!-- 过滤器链定义:从上向下顺序执行,一般将/**放在最下边 -->
<property name="filters">
<map>
<entry key="authc" value-ref="formAuthenticationFilter" />
</map>
</property>
<property name="filterChainDefinitions">
<value>
<!-- 对静态资源设置匿名访问 -->
/pageResources/** = anon
<!-- 请求logout地址,shiro清除session -->
/user/logout = logout
<!-- /** = anon所有url都可以匿名访问 -->
/user/login = authc
/mform/* = authc
/me/* = authc
</value>
</property>
</bean>
<!-- securityManager -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="customRealm" />
<property name="cacheManager" ref="cacheManager" />
<property name="sessionManager" ref="sessionManager" />
</bean>
<!-- 缓存管理器 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:spring/shiro-ehcache.xml" />
</bean>
<!-- 会话管理器 -->
<bean id="sessionManager"
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="globalSessionTimeout" value="600000" />
<property name="deleteInvalidSessions" value="true" />
<property name="sessionIdCookieEnabled" value="true" />
<property name="sessionIdCookie" ref="sessionIdCookie" />
</bean>
<!-- 自定义form认证过滤器 -->
<!-- 基于form表单的身份验证过滤器,不配置也会注册过滤器,表单中的用户账号,密码及loginurl将采用默认值,建议配置 -->
<bean id="formAuthenticationFilter" class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter">
<property name="usernameParam" value="username" />
<property name="passwordParam" value="password" />
</bean>
<!-- 会话Cookie模板 -->
<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg name="name" value="xuxiaonan.session.id" />
</bean>
<!-- realm -->
<bean id="customRealm" class="cn.xuxiaonan.shiro.CustomRealm">
<property name="userService" ref="userService"/>
</bean>
<!-- <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="interceptor.loginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors> -->
</beans>
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" metadata-complete="true">
<display-name>Archetype Created Web Application</display-name>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<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>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</context-param>
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 -->
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring MVC servlet -->
<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/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 放弃了.do和.action的风格 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 过滤静态资源,从而不拦截 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
<url-pattern>/imgs/*</url-pattern>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.woff</url-pattern>
<url-pattern>*.tff</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/test/login.jsp</welcome-file>
</welcome-file-list>
</web-app>
注意:这里在web的配置文件中我使用了context-param标签,即为上面提到的冲突问题
shiro的配置文件:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="F:\develop\ehcache" />
<defaultCache
maxElementsInMemory="1000"
maxElementsOnDisk="10000000"
eternal="false"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
>
</defaultCache>
</ehcache>
-
引入对应的依赖
dao层引入entity实体层
同理:
service层引入实体层
serviceImpl层引入dao层与service层
**注意:**引入的依赖之间具有传递性
web层引入service层
-
代码编写:
dao层:
注意:这里在匹配xml时候的路径要尤为注意,否则会找不到xml
userDao.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="cn.xuxiaonan.dao.userDao"> <select id="getUsername" resultType="cn.xuxiaonan.entity.vuser" parameterType="String"> SELECT * FROM vuser_tb WHERE username = #{username} AND del_flag = 0; </select> <insert id="insertVuser" parameterType="cn.xuxiaonan.entity.vuser"> insert into vuser_tb values( null, #{username}, #{password}, #{email}, #{del_flag}, #{date} ); </insert> <select id="getUser" resultType="cn.xuxiaonan.entity.vuser" parameterType="String"> SELECT * FROM vuser_tb WHERE username = #{username} AND del_flag = 0; </select> <select id="getPermission" resultType="cn.xuxiaonan.entity.permission" parameterType="int"> SELECT permission_tb.permission FROM permission_tb JOIN ( roleperm_tb JOIN ( role_tb JOIN ( roleuser_tb JOIN vuser_tb ON roleuser_tb.userid = vuser_tb.id ) ON role_tb.id = roleuser_tb.roleid ) ON roleperm_tb.roleid = role_tb.id ) ON permission_tb.id = roleperm_tb.perid WHERE vuser_tb.id = #{id} </select> </mapper>
userDao:
package cn.xuxiaonan.dao; /** * * <p>Title:userDao</p> * <p>Description:TODO</p> * <p>PersonWeb:www.xuxiaonan.cn</p> * @author dinggc * @date 2018年9月7日下午4:55:31 * @version 2018 */ import java.util.List; import cn.xuxiaonan.entity.permission; import cn.xuxiaonan.entity.vuser; public interface userDao { public vuser getUsername(String username)throws Exception; public void insertVuser(vuser vuser)throws Exception; public vuser getUser(String username)throws Exception; public List<permission> getPermission(int id)throws Exception; }
entity层:
注意:由于在网络上传输所以要实现Serializable
BaseEntity:
package cn.xuxiaonan.entity;
import java.io.Serializable;
/**
*
* <p>Title:BaseEntity</p>
* <p>Description:TODO</p>
* <p>PersonWeb:www.xuxiaonan.cn</p>
* @author dinggc
* @date 2018年5月9日下午2:46:22
* @version 2018
*/
public class BaseEntity implements Serializable{
private String date;//创建信息日期
private int del_flag;//删除标记 0存在 1删除
public BaseEntity() {
super();
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getDel_flag() {
return del_flag;
}
public void setDel_flag(int del_flag) {
this.del_flag = del_flag;
}
}
permission:
package cn.xuxiaonan.entity;
import java.io.Serializable;
/**
*
* <p>Title:permission</p>
* <p>Description:TODO</p>
* <p>PersonWeb:www.xuxiaonan.cn</p>
* @author dinggc
* @date 2018年9月11日下午7:00:58
* @version 2018
*/
public class permission extends BaseEntity{
private int id;
private String permission;
private String describe;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
}
vuser:
package cn.xuxiaonan.entity;
import java.io.Serializable;
/**
*
* <p>Title:vuser</p>
* <p>Description:TODO</p>
* <p>PersonWeb:www.xuxiaonan.cn</p>
* @author dinggc
* @date 2018年9月7日下午4:33:19
* @version 2018
*/
public class vuser extends BaseEntity{
private int id;
private String username;
private String password;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
interface(service)层:
package cn.xuxiaonan.service;
import java.util.List;
import org.springframework.context.annotation.Lazy;
import cn.xuxiaonan.entity.permission;
import cn.xuxiaonan.entity.vuser;
/**
*
* <p>Title:userService</p>
* <p>Description:user接口</p>
* <p>PersonWeb:www.xuxiaonan.cn</p>
* @author dinggc
* @date 2018年10月28日下午5:13:55
*/
public interface userService {
public vuser getUsername(String username)throws Exception;
public vuser getUser(String username)throws Exception;
public List<permission> getPermission(int id)throws Exception;
}
serviceImpl层:
注意:@Service使用dubbo的注解,否则无法上传到注册中心
package cn.xuxiaonan.serviceimpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import cn.xuxiaonan.dao.userDao;
import cn.xuxiaonan.entity.permission;
import cn.xuxiaonan.entity.vuser;
import cn.xuxiaonan.service.userService;
/**
*
* <p>Title:userServiceImpl</p>
* <p>Description:用户接口实现类</p>
* <p>PersonWeb:www.xuxiaonan.cn</p>
* @author dinggc
* @date 2018年10月28日下午5:14:49
*/
@Service
public class userServicei implements userService{
@Autowired
userDao userdao;
@Override
public vuser getUsername(String username) throws Exception {
// TODO Auto-generated method stub
return userdao.getUsername(username);
}
@Override
public vuser getUser(String username) throws Exception {
// TODO Auto-generated method stub
System.out.println(userdao);
return userdao.getUser(username);
}
@Override
public List<permission> getPermission(int id) throws Exception {
// TODO Auto-generated method stub
return userdao.getPermission(id);
}
}
web层:
整合shiro:
自定义realm:
package cn.xuxiaonan.shiro;
import java.util.ArrayList;
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.SimpleAuthenticationInfo;
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 org.springframework.context.annotation.Lazy;
import com.alibaba.dubbo.config.annotation.Reference;
import cn.xuxiaonan.entity.permission;
import cn.xuxiaonan.entity.vuser;
import cn.xuxiaonan.service.userService;
/**
*
* <p>Title:CustomRealm</p>
* <p>Description:自定义realm</p>
* <p>PersonWeb:www.xuxiaonan.cn</p>
* @author dinggc
* @date 2018年9月11日下午6:51:21
*/
public class CustomRealm extends AuthorizingRealm{
private userService userService;
public void setUserService(userService userService) {
this.userService = userService;
}
//设置realm的名称
@Override
public void setName(String name) {
super.setName("customRealm");;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//token是用户输入的
//第一步从token中取出身份信息
String userCode = (String)token.getPrincipal();
vuser vuser = null;
try {
vuser = userService.getUser(userCode);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//模拟从数据库查询密码
//如果查询不到返回null
//数据库中用户账号是zhangsan
if(vuser==null) {
return null;
}
String PIN = vuser.getPassword();
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(vuser, PIN,this.getName());
return simpleAuthenticationInfo;
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//从principals获取主身份信息
//将getPrimaryPrincipal方法返回值转为真实身份信息(在上边的doGetAuthecticationInfo认证通过填充到SimpleAuthenticationInfo)
vuser vuser = (vuser) principals.getPrimaryPrincipal();
//根据信息获取权限信息
//连接数据库。。。
//模拟从数据库获取到数据
List<permission> permissions = null;
try {
permissions = userService.getPermission(vuser.getId());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//...
List<String> permission = new ArrayList<String>();
for(permission syspermission:permissions) {
permission.add(syspermission.getPermission());
}
//查询到权限数据,返回
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//将上边查询到授权信息填充到simpleAuthorizationInfo对象中
simpleAuthorizationInfo.addStringPermissions(permission);
return simpleAuthorizationInfo;
}
}
controller层:
注意:这里的controller应使用@Reference来引用服务
package cn.xuxiaonan.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
*
* <p>Title:mainController</p>
* <p>Description:TODO</p>
* <p>PersonWeb:www.xuxiaonan.cn</p>
* @author dinggc
* @date 2018年11月3日下午10:15:18
* @version 2018
*/
@Controller
@RequestMapping(value="/mform")
public class mainController {
@RequestMapping(value="/entrance")
public String entrance() {
return "test/main";
}
}
package cn.xuxiaonan.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.dubbo.config.annotation.Reference;
import cn.xuxiaonan.entity.permission;
import cn.xuxiaonan.entity.vuser;
import cn.xuxiaonan.service.userService;
/**
*
* <p>Title:userController</p>
* <p>Description:用户层</p>
* <p>PersonWeb:www.xuxiaonan.cn</p>
* @author dinggc
* @date 2018年10月28日下午5:21:45
*/
@Controller
@RequestMapping("/user")
public class userController {
@Reference
userService userService;
@RequestMapping("/login")
public String login(HttpServletRequest request,Model model)throws Exception{
//如果登录失败从request中获取认证异常信息,shiroLoginFailure就是shiro异常lei的全限定名
String winfo = null;
String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
//根据shiro返回的异常类路径判断,抛出指定异常信息
if (exceptionClassName!=null){
if(UnknownAccountException.class.getName().equals(exceptionClassName)) {
//最终会抛给异常处理器
winfo = "userisnotexist";
}else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
winfo = "oneiswrong";
}else{
winfo = "other";
}
}else {
winfo = "success";
}
model.addAttribute("winfo",winfo);
return "test/login";
}
@RequestMapping("/usercheck")
@ResponseBody
public Object usercheck(HttpServletRequest request)throws Exception{
String username = request.getParameter("username");
vuser vuser = userService.getUsername(username);
if(vuser!=null) {
return "no";
}else {
return "yes";
}
}
@RequestMapping("/backentrance")
public String backentrance() {
return "test/login";
}
@RequestMapping(value ="/test")
public String test() throws Exception {
vuser vuser = userService.getUser("dinggc");
System.out.println(vuser);
return "/test/login";
}
}
-
前端代码的编写:
pageResource与test文件夹已提供