獲取spring security用戶相關信息

 

在JSP中得到java

 

使用spring security的標籤庫web

 

在頁面中引入標籤spring

 

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
而後
<div> username : <sec:authentication property="name"/></div>
便可顯示當前用戶。session

 

 

 

 

 

在程序中得到(方式一)app

 

UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();加密

 

實際運用中發現得到的Authentication爲null。仔細看了下源代碼發現,若是想用上面的代碼得到當前用戶,必須在spring spa

 

security過濾器執行中執行,不然在過濾鏈執行完時org.springframework.security.web.context.SecurityContextPersistenceFilter類會code

 

調用SecurityContextHolder.clearContext();而把SecurityContextHolder清空,因此會獲得null。對象

 

 

 

在程序中得到(方式二)blog

 

通過spring security認證後,security會把一個SecurityContextImpl對象存儲到session中,此對象中有當前用戶的各類資料

 

 

package com.devjav.spring;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/home.do", method = RequestMethod.GET)
    public String home(HttpServletRequest request, HttpServletResponse response, Locale locale) {
        logger.info("Welcome User home! The client locale is {}.", locale);

        /*
         * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
         */
        SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession()
                .getAttribute("SPRING_SECURITY_CONTEXT");
        // 登陸名
        System.out.println("Username:" + securityContextImpl.getAuthentication().getName());
        // 登陸密碼,未加密的
        System.out.println("Credentials:" + securityContextImpl.getAuthentication().getCredentials());
        WebAuthenticationDetails details = (WebAuthenticationDetails) securityContextImpl.getAuthentication()
                .getDetails();
        // 得到訪問地址
        System.out.println("RemoteAddress" + details.getRemoteAddress());
        // 得到sessionid
        System.out.println("SessionId" + details.getSessionId());
        // 得到當前用戶所擁有的權限
        List<GrantedAuthority> authorities = (List<GrantedAuthority>) securityContextImpl.getAuthentication()
                .getAuthorities();
        for (GrantedAuthority grantedAuthority : authorities) {
            System.out.println("Authority" + grantedAuthority.getAuthority());
        }
        /*
         * ???????????????????????????????????????????????????????????????????
         */

        return "home";
    }

    @RequestMapping(value = "/admin/home.do", method = RequestMethod.GET)
    public String Adminhome(Locale locale) {
        logger.info("Welcome to Admin home! The client locale is {}.", locale);

        return "adminhome";
    }

    @RequestMapping(value = "/accessdenied.do", method = RequestMethod.GET)
    public String accessDenied() {
        logger.info("Access deniend.");
        return "accessdenied";
    }
}
相關文章
相關標籤/搜索