1、针对eclipse(jdk1.8 tomcat8.0),创建SpringMVC工程,File->New->Dynamic Web Project
project name 随意,->finish
2、WebContent下WEB-INF下lib添加项目所需jar包,附jar包贴图
构建路径:右击项目->Build Path ->Configure Build Path ->Libraries ->Add JRES (选择项目、lib下jar包)依次选择 ok
3、WEB-INF下创建web.xml初始化配置信息,具体看注释
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_3_1.xsd"
id="WebApp_ID" version="3.1">
3、创建SpringMVC-servlet.xml
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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
4、创建applicationContext-security.xml配置文件
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.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
5、创建登录controller
package com.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController {
@RequestMapping("/login")
public ModelAndView login(String username, String password){
if(username.equals("username") && password.equals("password")){
System.out.println(username+"登陆成功!");
return new ModelAndView("view/loginSuccess","username",username);
}else{
return new ModelAndView("view/loginError","username",username);
}
}
@RequestMapping("/list")
public ModelAndView list(String username){
if(username!=null && !"".equals(username)){
return new ModelAndView("view/list","username",username);
}else{
return new ModelAndView("view/loginSuccess","username",username);
}
}
@RequestMapping("/logout")
public ModelAndView logout(){
return null;
}
}
6、创建CsrfSecurityRequestMatcher.java、此时HttpServletRequest会报编译错误(HttpServletRequest cannot be resolved to a type)、右击项目->Build Path ->Configure Build Path ->Libraries ->Add Library ->Server Runtime ->next (选择tomcat8.0)->finish
package com.demo.security;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
public class CsrfSecurityRequestMatcher implements RequestMatcher{
/*自定义不需要拦截的请求方式*/
private Pattern allowedMethods = Pattern.compile("^(GET|HEAD|TRACE|OPTIONS)$");
/*有rest服务时用*/
private RegexRequestMatcher unprotectedMatcher = new RegexRequestMatcher("^/rest/.*", null);
@Override
public boolean matches(HttpServletRequest request) {
if(allowedMethods.matcher(request.getMethod()).matches()){
return false;
}
return !unprotectedMatcher.matches(request);
}
}
7、WebContent下创建login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
8、WebContent下创建view文件夹、创建
1)loginSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
2)loginError.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Sorry,没有${username }这个用户!
请重新登录!
3)list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
9、项目结构
登录google浏览器登录F12模式->Network看token值、
登录成功查看用户列表 token
附源码:
https://download.csdn.net/download/diaofeiyang/10796342
链接:https://pan.baidu.com/s/10YUmUSyaolILgAOGIuwfVA
提取码:rk2o
均可下载