spring mvc 4.1.1集成spring-security3.2.10 demo

 

1、针对eclipse(jdk1.8 tomcat8.0),创建SpringMVC工程,File->New->Dynamic Web Project

project name 随意,->finish

 

spring mvc 4.1.1集成spring-security3.2.10 demo_第1张图片

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2、WebContent下WEB-INF下lib添加项目所需jar包,附jar包贴图

构建路径:右击项目->Build Path ->Configure Build Path ->Libraries ->Add JRES (选择项目、lib下jar包)依次选择 ok

spring mvc 4.1.1集成spring-security3.2.10 demo_第2张图片

3、WEB-INF下创建web.xml初始化配置信息,具体看注释


 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_3_1.xsd"
 id="WebApp_ID" version="3.1">
  SpringMVC
  
 
 
      contextConfigLocation
     
      /WEB-INF/SpringMVC-servlet.xml,/WEB-INF/applicationContext-security.xml
 

 
      org.springframework.web.context.ContextLoaderListener
 

  
 
 
 
      springSecurityFilterChain 
      org.springframework.web.filter.DelegatingFilterProxy
 

 
      springSecurityFilterChain
      /*
 

   
 
 
     SpringMVC
     org.springframework.web.servlet.DispatcherServlet
     1
 

 
 
     SpringMVC
     /
 

3、创建SpringMVC-servlet.xml


    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: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">
  
   
            class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
            class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
   
   
     
     
   
       
       
       
   

4、创建applicationContext-security.xml配置文件

 
    xmlns:b="http://www.springframework.org/schema/beans" 
    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">
                        
   
    
   
   
    
   
   
       
        
       
       
       
       
        
       
       
        
       
       
       
       
     
              
   

        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
   
 

      jsr250-annotations="enabled" secured-annotations="enabled">
 
   
   
       
         
            
                 
                 
            

       
 
   

    

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"%>



Spring 4.1 MVC demo


    
    


    
        
    


8、WebContent下创建view文件夹、创建

1)loginSuccess.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


<%-- ${username } --%>
    


    
        
        
    


2)loginError.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here

    Sorry,没有${username }这个用户!
   

    请重新登录!


3)list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


${username}

9、项目结构

spring mvc 4.1.1集成spring-security3.2.10 demo_第3张图片

 

登录google浏览器登录F12模式->Network看token值、

spring mvc 4.1.1集成spring-security3.2.10 demo_第4张图片

登录成功查看用户列表 token

spring mvc 4.1.1集成spring-security3.2.10 demo_第5张图片

附源码:

https://download.csdn.net/download/diaofeiyang/10796342

链接:https://pan.baidu.com/s/10YUmUSyaolILgAOGIuwfVA 
提取码:rk2o 

均可下载

你可能感兴趣的:(spring)