zoukankan      html  css  js  c++  java
  • spring security+freemarker获取登陆用户的信息

    spring security+freemarker获取登陆用户的信息

    目标页面之间获取

     ${Session.SPRING_SECURITY_CONTEXT.authentication.principal.username}

    其他参考

    Spring Security判断用户是否已经登录

    <c:if test="${pageContext.request.userPrincipal.name != null}">
        <label>
         Hi ${pageContext.request.userPrincipal.name} ! Welcome to our site
        </label>
    </c:if>
    
    <c:choose>
      <c:when test="${pageContext.request.userPrincipal.authenticated}">Show something</c:when>
      <c:otherwise>Show something else</c:otherwise>
    </c:choose>
    

      

    方法二、检查角色

    <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    
        <sec:authorize access="hasAnyAuthority('ROLE_ADMIN', 'ROLE_USER')" var="isAuthenticated">
        </sec:authorize>
    
        <c:out value="${isAuthenticated}"/>
    

      

    和这个

    <sec:authorize access="hasAnyRole('ROLE_ADMIN')">
        <a href="delete/${file.id}">Delete</a>
    </sec:authorize>
    

      

    方法三、 还是查询用户

    Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    if (!(auth instanceof AnonymousAuthenticationToken)) { 
         // do something...
    }
    

      

    方法四、 使用标签库

    <%@taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>
    <sec:authorize access="isAuthenticated()">
        <% response.sendRedirect("main"); %>
    </sec:authorize>
    

      

    方法五、 使用注解

    需要:<global-method-security secured-annotations="enabled" />

    @Secured("ROLE_ADMIN")
    @RequestMapping(params = "onlyForAdmins")    
    public ModelAndView onlyForAdmins() {
        ....
    }
    
     @PreAuthorize("isAuthenticated()")
     @RequestMapping(params = "onlyForAuthenticated")
     public ModelAndView onlyForAuthenticatedUsers() {
         ....
     }
    

      

    方法六、 编程

    SecurityContextHolder.getContext().getAuthentication() != null &&
     SecurityContextHolder.getContext().getAuthentication().isAuthenticated() &&
     //when Anonymous Authentication is enabled
     !(SecurityContextHolder.getContext().getAuthentication() 
              instanceof AnonymousAuthenticationToken) 
    
    
    if (SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) {
      System.out.println("LOGGED IN");
      } else {
      System.out.println("NOT LOGGED IN");
    }
    
    
    if (!SecurityContextHolder.getContext().getAuthentication().getName().
      equals("anonymousUser")) {
      System.out.println("LOGGED IN");
      } else {
      System.out.println("NOT LOGGED IN");
    }
    

      

    https://www.jianshu.com/p/70569b64f9a9

    https://blog.csdn.net/when_where_who/article/details/53610345

  • 相关阅读:
    关于gtk的GCond
    位运算符及其应用
    登陆新浪微博&批量下载收藏内容[Python脚本实现]
    海量数据处理算法—Bloom Filter
    海量数据处理算法—BitMap
    VB.NET机房收费系统——组合查询
    非官方的gstreamer学习资料及概念摘要
    [Python入门及进阶笔记00]写在前面(目录/书籍/学习路线/其他)
    [JAVA][Eclipse]JVM terminated. Exit code=13
    介绍一个android开源文件选择对话框:androidfiledialog
  • 原文地址:https://www.cnblogs.com/achengmu/p/9708498.html
Copyright © 2011-2022 走看看