Spring Security 入门教程-01-Hello World

Spring Security

对于 web 安全的控制,以前接触使用过 Shiro。

刚好最近在学习整理 Spring 相关的技术,就学习一下 Spring Security。

是什么?

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications.

Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements.

Hello World

看了下官方的文档 5.0.0.RELEASE doc,
案例依赖于 gradle,一直以来使用的是 maven。

Spring Security入门程序示例

测试环境

$ java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

$ mvn -v
Apache Maven 3.3.9

项目结构

完整代码下载地址

├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── ryo
    │   │           └── spring
    │   │               └── security
    │   │                   └── hello
    │   │                       └── controller
    │   │                           └── HelloWorldController.java
    │   ├── resources
    │   │   ├── application-mvc.xml
    │   │   ├── application-security.xml
    │   │   └── application.xml
    │   └── webapp
    │       └── WEB-INF
    │           ├── pages
    │           │   ├── admin.jsp
    │           │   └── hello.jsp
    │           └── web.xml

文件内容

  • pom.xml

引入指定的 jar 和插件。



    
        spring-security
        com.ryo
        1.0-SNAPSHOT
    
    4.0.0

    spring-security-hello
    war


    

        

            
            
                io.spring.platform
                platform-bom
                2.0.8.RELEASE
                pom
                import
            

        
    

    

        
        
            org.springframework
            spring-core
        
        
            org.springframework
            spring-web
        
        
            org.springframework
            spring-webmvc
        

        
        
            org.springframework.security
            spring-security-core
        
        
            org.springframework.security
            spring-security-web
        
        
            org.springframework.security
            spring-security-config
        

        
        
            javax.servlet
            jstl
        

    


    

        
            
            
               org.apache.tomcat.maven
               tomcat7-maven-plugin
               2.2
               
                   8080
                   /
                   UTF-8
               
            
        

    


  • admin.jsp & hello.jsp

两个 jsp 页面,内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page session="true"%>


    


标题: ${title}

消息 : ${message}

欢迎: ${pageContext.request.userPrincipal.name} | " > Logout

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>



    


标题:${title}

消息:${message}

  • web.xml

用于指定 web 项目相关的配置,内容如下:



    Archetype Created Web Application

    
    
        contextConfigLocation
        classpath:application.xml
    

    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    
    
        encodingFilter
        /*
    

    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:application-mvc.xml
        
        1
    

    
        springmvc
        /
    


    
    
        springSecurityFilterChain
        org.springframework.web.filter.DelegatingFilterProxy
        
    

    
        springSecurityFilterChain
        /*
    


  • application.xml

spring 配置文件,引入了 application-mvc.xmlapplication-security.xml




    
    


  • application-mvc.xml

spring mvc 的配置:




    
    
        
        
    


    

    
    
        
        
        
    


  • application-security.xml

spring security 的配置:



    
        
    

    
        
            
                
            
        
    


  • HelloWorldController.java

简单的页面控制类:

package com.ryo.spring.security.hello.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/")
public class HelloWorldController {

    /**
     * 返回 hello 视图
     * @return
     */
    @RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
    public ModelAndView welcomePage() {
        ModelAndView model = new ModelAndView();
        model.addObject("title", "Spring Security Hello World");
        model.addObject("message", "This is welcome page!");
        model.setViewName("hello");
        return model;
    }

    /**
     * 返回 admin 视图
     * @return
     */
    @RequestMapping(value = "/admin**", method = RequestMethod.GET)
    public ModelAndView adminPage() {
        ModelAndView model = new ModelAndView();
        model.addObject("title", "Spring Security Hello World");
        model.addObject("message", "This is protected page!");
        model.setViewName("admin");
        return model;
    }
}

运行访问

直接运行 插件 tomcat7:run 启动服务,或者手动部署到 tomcat,此处不再赘述。

启动日志如下:

[INFO] Scanning for projects...
...
[INFO] Running war on http://localhost:8080/
...
[INFO] Mapped "{[/admin**],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView com.ryo.spring.security.hello.controller.HelloWorldController.adminPage()
[INFO] Mapped "{[/ || /welcome**],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView com.ryo.spring.security.hello.controller.HelloWorldController.welcomePage()
...
信息: Starting ProtocolHandler ["http-bio-8080"]
  • 首页

直接浏览器访问 http://localhost:8080/,首页内容如下:

标题:Spring Security Hello World
消息:This is welcome page!
  • admin

如果我们尝试访问 http://localhost:8080/admin,会进入登录页面。

用户名密码和我们 application-security.xml 文件中配置一致。

一、输入错误信息

页面内容如下:

Your login attempt was not successful, try again.

Reason: Bad credentials

二、输入正确信息

标题: Spring Security Hello World
消息 : This is protected page!
欢迎: ryo | Logout

好,到这里入门就介绍完毕。

后续会继续学习,并记录下笔记。

你可能感兴趣的:(Spring Security 入门教程-01-Hello World)