GithubHelp home page GithubHelp logo

Spring Security 文档阅读笔记 about blog HOT 1 OPEN

zxy16305 avatar zxy16305 commented on July 26, 2024
Spring Security 文档阅读笔记

from blog.

Comments (1)

zxy16305 avatar zxy16305 commented on July 26, 2024

3.2 开始支持java配置

WebSecurityConfigurerAdapter.configure(Httpsecurity http) 的默认配置

  1. 所有请求都需要认证
  2. 表单登录认证开放
  3. HTTP认证开放

调用/logout会发生的事情(默认情况下)

  1. 清除HTTP的session
  2. 清除配置的rememberMe
  3. 清除SecurityContextHolder
  4. 重定向到/login?logout

LogoutSuccessHandler被指定的时候,logoutSuccessUrl( )会被忽略。
HttpStatusReturningLogoutSuccessHandler用在restful的api下


spring security基本架构

  • SecurityContextHolder 用来存储SecurityContext
  • SecurityContext 用来保存Authentication 和一些特定的请求信息
  • Authentication 用来代表 Spring security的主要数据
  • GrantedAuthority 反映应用范围内的权限认证
  • UserDetails 从数据源(DAO或者另外的数据),获取必要的认证信息
  • UserDetailsService : 传入username,获取UserDetails

SecurityContextHolder存储方式

  1. SecurityContextHolder.MODE_GLOBAL : 全局存储
  2. SecurityContextHolder.MODE_INHERITABLETHREADLOCAL :线程与其产生的子线程共享存储
  3. SecurityContextHolder.MODE_THREADLOCAL : 线程之间单独存储

读取当前的user信息

SecurityContextHolder.getContext().getAuthentication().getPrincipal(); //用户信息
SecurityContextHolder.getContext().getAuthentication().getAuthorities();//用户权限


自定义user鉴别

实现UserDetailsService
同时在configure( AuthenticationManagerBuilder auth )中,配置auth.userDetailsService(myUserDetailsService);


后台实现登录(伪)

基于其存储原理,可进行以下的操作(官方Demo, PS : 官方为了展示security的内部流程而写的):

  • 实现AuthenticationManager ,在其方法中写入权限 ;调用后得到完整的Authentication (官方demo是在里面验证了password的正确性)
  • SecurityContextHolder.getContext().setAuthentication(authentication ); 写入权限

Web Application的认证

  • 主要的类 : ExceptionTranslationFilter,AuthenticationEntryPoint
  • ExceptionTranslationFilter :

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.