SpringSecurity原理有哪些

本篇内容介绍了“SpringSecurity原理有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:申请域名、网页空间、营销软件、网站建设、清流网站维护、网站推广。

前置知识

Spring Security主要是服务于Web应用,Spring Security也主要是通过Java的Filter来实现。

Java的Web应用两大核心就是:

  1. Servlet

  2. Filter

很多朋友可能已经忘了在没有SpringMVC之前被java的web.xml配置支配的恐惧。

如果对此没有印象的朋友,或者完全没有使用过的朋友,可以看下面的web.xml配置回忆一下,看还能不能记得每个配置的含义。



    terrible
    
        index.html
    
    
        EcodingFilter
        EcodingFilter
        vip.mycollege.filter.EnCodeFilter
        
            EncodeCoding
            UTF-8
        
    
    
        EcodingFilter
        hello
        *
    
    
        hello
        
    
    
        hello
        /*
    

上面的配置还是只是一个简单的Servet和一个Filter。

Java Web的整个流程还简单,就是Servelt容器,也就是Java Web服务器,如,Tomcat、Jetty等启动的时候回去加载web.xml文件,解析里面的配置,主要是Servlet和Filter,把url和Servlet、Filter对应上,这样当接收都一个url请求的时候,就交个对应的Filter和Servlet去处理。

先Filter后Servlet的,Filter可以有多个,是链式结构,有优先级。

web.xml配置方式的最大的问题就是,url映射配置复杂,稍不小心就404。

后来有了SpringMVC就好了一些,只需要配置一个Servlet,都交给org.springframework.web.servlet.DispatcherServlet这个Servlet统一处理url的映射分发,Spring自己去解析Controller注解,把url和对应的方法对应,不用自己去配置url映射了。

再后来有了SpringBoot,简直就起飞了,DispatcherServlet都不需要配置了,开箱即用。

虽然,不需要手动配置了,原理还是要了解一点,这里我们只需要记住,Java的Web请求都是要先过Filter的,而这是Spring Security施展身手的地方。

简单的示例

如果一上来就直接说一下抽象概念,很多朋友可能就放弃治疗了。所以,我们先来一个简单的入门级示例,在门口看一下Spring Security是个啥东西。

SpringSecurity原理有哪些

pom依赖

首先,要引入Spring Security依赖,如果使用SpringBoot,引入非常简单



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.5
        
    
    vip.oschool
    spsecurity
    0.0.1-SNAPSHOT
    spsecurity
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-security
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

配置文件

server:
  port: 8081

spring:
    security:
      user:
        name: tim
        password: 111111

上面配置了一个用户tim,密码是111111,如果不配置,Spring Security会使用默认用户,每次会生成随机密码。

控制器

添加一个简单的控制器

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/index")
public class IndexController {

    @RequestMapping("/hello")
    public String hello(){
        return "Hello World";
    }
}

启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StartApplication {

	public static void main(String[] args) {
		SpringApplication.run(StartApplication.class, args);
	}
}

登录界面

启动之后,直接访问:http://localhost:8081/index/hello

然后,就会被重定向到:http://localhost:8081/login

SpringSecurity原理有哪些

需要输入用户名和密码,这个过程有一个专有名词叫:认证,Authentication

“SpringSecurity原理有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


网站名称:SpringSecurity原理有哪些
当前链接:http://myzitong.com/article/ppsedo.html