Spring4.X + Spring MVC + Mybatis3 零配置应用开发框架搭建详解(3) - 实现最基本的登录处理
基础框架搭建完成后,我们开始进行Spring + SpringMVC + Mybatis的集成,来完成登录功能的处理。我们根据数据流向,来创建相应的内容,先看看大致的数据流向图:
根据上面的数据流向图,需要创建用户登录页面、登录成功页面,权限管理模块、控制层、服务层、数据库持久层,下面我们分别来创建对应的内容:
首先修改【webapp】下的【login.jsp】,添加一个需要输入登录信息的表单,然后在【WEB-INF】下创建【pages】目录,在【pages】目录下创建【index】目录,在【index】目录下创建【index.jsp】,不要问为什么创建那么多文件目录和层级,太麻烦,因为这是我的习惯,真正做项目后层级划分的好处以后你就懂。
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
登录
系统登录
用户名密码错误!
您已注销!
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
首页
首页
登录成功
首先在web项目下的pom.xml中添加公共的依赖库,以保证配置重用。
下面主要添加的是Spring核心的几个公用依赖包和日志包,以及工具类。插件中主要配置指定编译JDK的版本,以及资源文件的配置。
提示:添加依赖包的时候要注意设置Maven源的事,如果你自己设置的源出现问题,可以看看我的这篇博文:Maven国内源设置 - OSChina国内源失效了,别更新了
【Web项目下的pom.xml】
4.0.0
com.aitongyi.web
web
1.0-SNAPSHOT
pom
back
dao
bean
cache
service
task
org.springframework
spring-core
4.1.6.RELEASE
org.springframework
spring-context
4.1.6.RELEASE
org.springframework
spring-web
4.1.6.RELEASE
org.springframework
spring-beans
4.1.6.RELEASE
org.springframework
spring-jdbc
4.1.6.RELEASE
org.springframework
spring-context-support
4.1.6.RELEASE
org.quartz-scheduler
quartz
2.2.2
org.quartz-scheduler
quartz-jobs
2.2.2
ch.qos.logback
logback-core
1.1.3
ch.qos.logback
logback-classic
1.1.3
ch.qos.logback
logback-access
1.1.3
org.slf4j
slf4j-api
1.6.6
org.slf4j
jcl-over-slf4j
1.6.6
org.apache.commons
commons-lang3
3.3
commons-io
commons-io
2.4
mysql
mysql-connector-java
5.1.6
org.mybatis
mybatis
3.3.0
org.mybatis
mybatis-spring
1.2.3
org.apache.commons
commons-dbcp2
2.1.1
commons-fileupload
commons-fileupload
1.2.2
org.aspectj
aspectjweaver
1.8.6
javax.servlet
jstl
1.2
javax.servlet
javax.servlet-api
3.1.0
provided
central
Central Repository
http://repo1.maven.org/maven2/
false
jboss-public-repository-group
JBoss Public Repository Group
http://repository.jboss.org/nexus/content/groups/public/
default
true
never
true
never
jboss-deprecated
JBoss Deprecated
https://repository.jboss.org/nexus/content/repositories/deprecated/
default
true
never
false
jboss-maven2-brew
JBoss Maven 2 Brew Repository
http://repository.jboss.org/maven2-brew/
default
true
never
false
io.spring.repo.maven.release
http://repo.spring.io/release/
false
io.spring.repo.maven.milestone
http://repo.spring.io/milestone/
false
org.apache.maven.plugins
maven-compiler-plugin
3.1
1.7
true
true
UTF-8
${project.basedir}/src/main/java
org.apache.maven.plugins
maven-resources-plugin
2.6
UTF-8
然后我们再来配置【back】模块中pom.xml的内容:
web
com.aitongyi.web
1.0-SNAPSHOT
4.0.0
back
war
UTF-8
com.aitongyi.web
dao
${project.version}
com.aitongyi.web
bean
${project.version}
org.springframework
spring-webmvc
4.1.6.RELEASE
org.springframework.security
spring-security-web
4.0.2.RELEASE
org.springframework.security
spring-security-config
4.0.2.RELEASE
org.springframework.security
spring-security-taglibs
4.0.2.RELEASE
org.springframework
spring-context-support
4.1.6.RELEASE
org.quartz-scheduler
quartz
2.2.2
org.quartz-scheduler
quartz-jobs
2.2.2
javax.servlet
jstl
1.2
javax.servlet
javax.servlet-api
3.1.0
provided
commons-io
commons-io
2.4
commons-fileupload
commons-fileupload
1.2.2
org.aspectj
aspectjweaver
1.8.6
mysql
mysql-connector-java
5.1.6
org.mybatis
mybatis
3.3.0
org.mybatis
mybatis-spring
1.2.3
org.apache.commons
commons-dbcp2
2.1.1
在【back】项目的【src/main/java】目录下创建【com.aitongyi.web.back.conf】配置代码包、【com.aitongyi.web.back.controller】控制层代码包;
在【dao】项目的【src/main/java】目录下创建【com.aitongyi.web.dao.conf】配置代码包、【com.aitongyi.web.dao.mapper】持久层代码包;
在【service】项目的【src/main/java】目录下创建【com.aitongyi.web.service】服务层代码包;
在【bean】项目的【src/main/java】目录下创建【com.aitongyi.web.bean】模型代码包 ;在【back】项目【com.aitongyi.web.back.conf】包中创建五个配置对象:
【BackConfig.java】:负责管理基本配置信息
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
@Configurable
@PropertySource(value={"classpath:back.properties"})
public class BackConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean(name="schedulerFactoryBean")
public SchedulerFactoryBean schedulerFactoryBean(){
return new SchedulerFactoryBean();
}
}
【MvcConfig.java】:负责处理SpirngMVC的配置信息以及组件的加载
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@EnableWebMvc
@EnableAspectJAutoProxy
@EnableScheduling
@ComponentScan(basePackages = {"com.aitongyi.web.back.controller","com.aitongyi.web.service"})
@MapperScan("com.aitongyi.web.dao.mapper")
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
/**
* jsp视图解析器
* @return
*/
@Bean
public InternalResourceViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(org.springframework.web.servlet.view.JstlView.class);
bean.setPrefix("/WEB-INF/pages/");
bean.setSuffix(".jsp");
return bean;
}
/**
* 公共部分解析器
* @return
*/
@Bean(name="multipartResolver")
public CommonsMultipartResolver commonsMultipartResolver(){
CommonsMultipartResolver common = new CommonsMultipartResolver();
common.setMaxUploadSize(10 * 1024 * 1024);//10M
return common;
}
}
【SecurityConfig.java】:负责安全相关的配置处理,其中有一段代码:
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(md5Encoder);
这是SpringSecurity安全框架自动处理的逻辑,首先是通过username查询users表中是否有记录,然后通过将密码进行MD5加密,去跟数据库中的密码比对,如果相同就让用户执行configure方法中配置的登陆策略。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CsrfFilter;
import org.springframework.security.web.header.HeaderWriter;
import org.springframework.security.web.header.HeaderWriterFilter;
import org.springframework.web.filter.CharacterEncodingFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private static Md5PasswordEncoder md5Encoder = new Md5PasswordEncoder();
@Autowired
private DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(md5Encoder);
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resource/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// 将login.jsp定为登陆页面,只处理/login这个请求
.formLogin().loginPage("/login.jsp").and().formLogin().loginProcessingUrl("/login")
// 如果登陆成功就跳转到/home这个地址,如果失败就跳转到/?error=1
.and().formLogin().defaultSuccessUrl("/home").and().formLogin().failureUrl("/?error=1");
// 这里配置的是登出的请求
http.logout().logoutUrl("/logout")
// 登陆成功后跳转的地址,以及删除的cookie名称
.and().logout().logoutSuccessUrl("/")
.and().logout().deleteCookies("JSESSIONID");
// 配置记住我的过期时间
http.rememberMe().tokenValiditySeconds(1209600)
.and().rememberMe().rememberMeParameter("remember-me");
CharacterEncodingFilter encodeFilter = new CharacterEncodingFilter();
encodeFilter.setEncoding("utf-8");
encodeFilter.setForceEncoding(true);
http.addFilterBefore(encodeFilter, CsrfFilter.class); // 放在csrf filter前面
http.headers().disable();
HeaderWriter headerWriter = new HeaderWriter() {
public void writeHeaders(HttpServletRequest request, HttpServletResponse response) {
response.setHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
response.setHeader("Expires", "0");
response.setHeader("Pragma", "no-cache");
response.setHeader("X-Frame-Options", "SAMEORIGIN");
response.setHeader("X-XSS-Protection", "1; mode=block");
response.setHeader("x-content-type-options", "nosniff");
}
};
List headerWriterFilterList = new ArrayList<>();
headerWriterFilterList.add(headerWriter);
HeaderWriterFilter headerFilter = new HeaderWriterFilter(headerWriterFilterList);
http.addFilter(headerFilter);
}
}
【WebApplicationInitializer.java】:这是整个项目的核心。Servlet3.0规范,支持将web.xml相关配置也硬编码到代码中[servlet,filter,listener,等等],并由javax.servlet.ServletContainerInitializer的实现类负责在容器启动时进行加载,
spring提供了一个实现类SpringServletContainerInitializer(在spring-web包中的org.springframework.web目录),该类会调用所有org.springframework.web.WebApplicationInitializer实现类的onStartup方法,将相关的组件注册到服务器;而我们的WebApplicationInitializer继承自AbstractAnnotationConfigDispatcherServletInitializer,而AbstractAnnotationConfigDispatcherServletInitializer就实现了org.springframework.web.WebApplicationInitializer的onStartup方法,所以WebApplicationInitializer就是整个项目的关键,我们的整个项目就是通过它来启动。
import com.aitongyi.web.dao.conf.DatabaseConfig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;
public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* 获取配置信息
* @return
*/
@Override
protected Class>[] getRootConfigClasses() {
return new Class[] { BackConfig.class, DatabaseConfig.class, SecurityConfig.class };
}
@Override
protected Class>[] getServletConfigClasses() {
return new Class[] { MvcConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Filter[] getServletFilters() {
return null;
}
}
【SecurityWebApplicationInitializer.java】:是安全方面的启动组件,与上面的WebApplicationInitializer继承关系类似,可以启动加载一些安全相关的配置和类。暂时不需要实现。
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
}
【com.aitongyi.web.back.controller.UserController】:用户访问控制器
import com.aitongyi.web.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 用户请求处理器
* Created by admin on 16/8/6.
*/
@Controller
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@RequestMapping(value = "/home", method = RequestMethod.GET)
@PreAuthorize("isAuthenticated()")// isAuthenticated 如果用户不是匿名用户就返回true
public String showHomePage() {
try {
userService.loadUserByUsername("admin");
logger.info("load user ");
}catch (Exception e){
logger.error(e.getLocalizedMessage(), e);
}
return "/index/index";
}
}
【日志文件】:logback.xml
/app/logs/back.%d{yyyy-MM-dd}.log
3
[%-5level][%d{HH:mm:ss}] - [%logger{15}] - %msg%n
UTF-8
[%-5level][%d{HH:mm:ss}] - [%logger] - %msg%n
UTF-8
【数据库配置】:back.properties
#========= Mysql ============
jdbc.driver = com.mysql.jdbc.Driver
db.url = jdbc:mysql://127.0.0.1/web?useUnicode=true&characterEncoding=UTF-8
db.username = root
db.password = 123456
db.maxtotal = 150
db.minidle = 40
db.maxidle = 60
由于配置了数据库信息,所以数据库是必须的,需要你安装一个MySQL,然后创建一个web数据库,创建一张users表,具体字段见下面数据库表创建文件:(登录密码是123456)
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`enabled` tinyint(1) NOT NULL,
`create_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `ix_username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
INSERT INTO `users` (`id`, `username`, `password`, `enabled`, `create_date`)
VALUES
(1,'admin','e10adc3949ba59abbe56e057f20f883e',1,'2016-06-24 13:17:23'),
(2,'user','e10adc3949ba59abbe56e057f20f883e',1,'2016-06-24 13:20:05');
另外还要创建一张权限表:
CREATE TABLE `authorities` (
`username` varchar(50) NOT NULL,
`authority` varchar(50) NOT NULL,
UNIQUE KEY `ix_auth_username` (`username`,`authority`),
CONSTRAINT `fk_authorities_users` FOREIGN KEY (`username`) REFERENCES `users` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into(username, authority) value('admin','ROLE_ADMIN');
String password = md5Encoder.encodePassword(明文密码, null);
【com.aitongyi.web.bean.User】:用户对象,主要是登陆时做安全验证使用。
import java.util.Date;
/**
* Created by admin on 16/8/8.
*/
public class User {
private Integer id;
private String username;
private String password;
private boolean enabled;
private Date createDate;
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
【pom.xml】
web
com.aitongyi.web
1.0-SNAPSHOT
4.0.0
bean
【DatabaseConfig.java】:数据库配置类
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
/**
* Created by admin on 16/8/8.
*/
@Configuration
@EnableTransactionManagement
public class DatabaseConfig {
private static final Logger logger = LoggerFactory.getLogger(DatabaseConfig.class);
@Value("${jdbc.driver}")
private String jdbcDriver;
@Value("${db.url}")
private String dbUrl;
@Value("${db.username}")
private String username;
@Value("${db.password}")
private String password;
@Value("${db.maxtotal}")
private Integer maxTotal;
@Value("${db.minidle}")
private Integer minIdle;
@Value("${db.maxidle}")
private Integer maxIdle;
@Bean(destroyMethod = "close")
public DataSource dataSource() {
logger.info("mysql url:"+dbUrl);
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(jdbcDriver);
dataSource.setUrl(dbUrl);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setMaxTotal(maxTotal);
dataSource.setMinIdle(minIdle);
dataSource.setMaxIdle(maxIdle);
return dataSource;
}
@Bean
public DataSourceTransactionManager txManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
return sessionFactory.getObject();
}
}
【 UserMapper.java 】:数据库持久层接口,主要是Mybatis管理
mport com.aitongyi.web.bean.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* 用户数据映射
* Created by admin on 16/8/8.
*/
public interface UserMapper {
@Select(value="select username,password,enabled from users where username = #{username}")
User loadUserByUsername(@Param("username") String username);
@Insert(value="insert into users (username, password, enabled, create_date) value(#{username},#{password},#{enabled},#{createDate})")
void saveUser(User user);
}
web
com.aitongyi.web
1.0-SNAPSHOT
4.0.0
dao
com.aitongyi.web
bean
${project.version}
【UserService】:用户服务接口
import com.aitongyi.web.bean.User;
import com.aitongyi.web.dao.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* 用户服务接口
*
*/
@Service
public class UserService {
private static final Logger logger = LoggerFactory.getLogger(UserService.class);
@Autowired
private UserMapper userMapper;
@Transactional
public User loadUserByUsername(String username) {
return userMapper.loadUserByUsername(username);
}
@Transactional
public void saveUser(User user) {
userMapper.saveUser(user);
// 测试异常后数据是否回滚
// getError();
}
private void getError() {
int i = 1 / 0;
logger.info("i:{}" , i);
}
}
右键选择项目,添加一个过滤文件,在文件中能匹配上的文件将不会通过版本控制软件进行版本控制。
到目前为止,项目的所有文件都已经创建完成,我们点击运行按钮,启动项目,输入用户名:admin,密码:123456,登录,就能看到登录成功
如果你会问,我们没有写登录验证的代码,为什么就自动登录了呢?你试着把密码输错,你会发现你登录不了了!就是这么神奇!
其实这是我们使用SpringSecurity安全框架的原因,安全框架自动会帮我们做一切登录验证的事!你看看日志中有这么一段:
Executing prepared SQL statement [select username,password,enabled from users where username = ?]
这句sql你没有写过吧?这就是安全框架自己做的!关于安全框架的事,就说到这里,展开了十篇文章不一定讲完,还是以后再写吧!
以上内容其实就已经搭建完成一个基本的零配置框架,今天就写到这里,由于时间的关系,我会继续补上以下内容:
1. 可以通过加入任务调度配置来执行任务调度。
2. 可以加入redis缓存来实现缓存管理。
3. 可以集成shiro安全框架,也可以自己写(个人觉得目前的安全框架文档不全、配置繁琐、使用复杂)
4. 登录页面、首页样式需要调整
(一)基本介绍
(二)基础框架搭建
(三)实现最基本的登录处理
(四)任务调度管理
(五)Redis缓存配置
(六)安全框架集成
(七) git版本源代码下载