zoukankan      html  css  js  c++  java
  • SecurityContextHolder

    SecurityContextHolder.getContext().getAuthentication().getPrincipal()

     
    SecurityContextHolder.getContext().getAuthentication().getPrincipal()
     
     

    安全包括两个主要操作。

      第一个被称为“认证”,是为用户建立一个他所声明的主题。主题一般式指用户,设备或可以在你系统中执行动作的其他系统。

      第二个叫“授权”,指的是一个用户能否在你的应用中执行某个操作,在到达授权判断之前,身份的主题已经由身份验证过程建立。

    1. 在web.xml文件中加入Filter声明

    Xml代码  
    1.   
    2. <</span>filter>  
    3.     <</span>filter-name>springSecurityFilterChain</</span>filter-name>  
    4.     <</span>filter-class>org.springframework.web.filter.DelegatingFilterProxy</</span>filter-class>  
    5. </</span>filter>  
    6. <</span>filter-mapping>  
    7.     <</span>filter-name>springSecurityFilterChain</</span>filter-name>  
    8.     <</span>url-pattern>/*</</span>url-pattern>  
    9. </</span>filter-mapping>  

    获得当前已认证的用户的名字(obtain the name of the currently authenticated user)
     (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    官方案例如下:

    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    if (principal instanceof UserDetails) {

      String username = ((UserDetails)principal).getUsername();

    } else {

      String username = principal.toString();

    }

    访问受保护的资源

  • 相关阅读:
    [Angular 2] Handle Reactive Async opreations in Service
    移动应用中的流设计
    Linux/Unix--设备类型
    [CF 276C]Little Girl and Maximum Sum[差分数列]
    Java多态
    Shiro(4)默认鉴权与自定义鉴权
    HDU4667(有错)
    weblogic8.1 登陆5 ip 限制
    [置顶] struts2+hibernate+spring整合(annotation版)
    hdu2159 二维完全背包
  • 原文地址:https://www.cnblogs.com/husfsh-16300/p/6656920.html
Copyright © 2011-2022 走看看