日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

sudo su su_Spring Security应用程序中的su和sudo

發布時間:2023/12/3 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sudo su su_Spring Security应用程序中的su和sudo 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

sudo su su

很久以前,我從事的項目具有很強大的功能。 有兩個角色:用戶和主管。 主管可以以任何方式更改系統中的任何文檔,而用戶則更受工作流約束的限制。 當普通用戶對當前正在編輯并存儲在HTTP會話中的文檔有疑問時,主管可以介入,切換到特殊的主管模式并繞過所有約束。 完全自由。 同一臺計算機,同一鍵盤,同一HTTP會話。 通過輸入秘密密碼,只有管理員可以設置的特殊標志。 主管完成后,他或她可以清除該標志并再次啟用常規約束。

此功能運行良好,但實施效果不佳。 每個單個輸入字段的可用性取決于該超級用戶模式標志。 使用isSupervisorMode()檢查在數十個地方污染了業務方法。 請記住,如果管理員只是使用普通憑據登錄,則此模式是隱式的,因此安全約束基本上是重復的。

當我們的應用程序可以高度自定義并具有大量安全角色時,就會出現另一個有趣的用例。 遲早您將面臨異常(正常, 錯誤 ),您無法復制具有不同權限的異常。 能夠以該特定用戶身份登錄并環顧四周可能是一個很大的勝利。 當然,您不知道用戶的密碼( 不是嗎? )。 類似UNIX的系統找到了解決此問題的方法: su ( 切換用戶 )和sudo命令。 出乎意料的是, Spring Security附帶了內置的SwitchUserFilter ,它在原則上模仿Web應用程序中的su 。 試一試吧!

您只需要聲明自定義過濾器:

<bean id="switchUserProcessingFilter"class="org.springframework.security.web.authentication.switchuser.SwitchUserFilter"><property name="userDetailsService" ref="userDetailsService"/><property name="targetUrl" value="/"/> </b:bean>

并在<http>配置中指向它:

<http auto-config="true" use-expressions="true"><custom-filter position="SWITCH_USER_FILTER" ref="switchUserProcessingFilter" /><intercept-url pattern="/j_spring_security_switch_user" access="hasRole('ROLE_SUPERVISOR')"/>...

而已! 請注意,我保護了/j_spring_security_switch_user URL模式。 您猜對了,這就是您以其他用戶身份登錄的方式,因此我們希望此資源得到良好的保護。 默認情況下,使用j_username參數名。 將上述更改應用于您的Web應用程序并以具有ROLE_SUPERVISOR的用戶ROLE_SUPERVISOR登錄后,可以簡單地瀏覽至:

/j_spring_security_switch_user?j_username=bob

假設存在這樣的用戶,您將自動以bob身份登錄。 此處不需要密碼。 模擬完他后,瀏覽到/j_spring_security_exit_user將恢復您以前的憑據。 當然,所有這些URL是可配置的。 參考文檔中未記錄SwitchUserFilter ,但謹慎使用時它是非常有用的工具。

確實具有強大的力量…… 。 像任何其他任意用戶一樣,甚至讓最受信任的用戶登錄也具有很大的風險。 想象一下在Facebook上的這種功能,這是不可能的! ( 很好…… )因此,跟蹤和審核成為一項主要要求。

我通常首先要做的是在Spring Security過濾器之后添加一個小的servlet過濾器,該過濾器將用戶名添加到MDC :

import org.slf4j.MDC;public class UserNameFilter implements Filter {@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();final String userName = authentication.getName();final String fullName = userName + (realName != null ? " (" + realName + ")" : "");MDC.put("user", fullName);try {chain.doFilter(request, response);} finally {MDC.remove("user");}}private String findSwitchedUser(Authentication authentication) {for (GrantedAuthority auth : authentication.getAuthorities()) {if (auth instanceof SwitchUserGrantedAuthority) {return ((SwitchUserGrantedAuthority)auth).getSource().getName();}}return null;}//... }

只要記住在 Spring Security 之后將其添加到web.xml 。 此時,您可以在logback.xml引用"user"鍵:

<pattern>%d{HH:mm:ss.SSS} | %-5level | %X{user} | %thread | %logger{1} | %m%n%rEx</pattern>

看到%X{user}代碼段? 每次登錄的用戶在系統中執行某些觸發日志語句的操作時,都會看到該用戶的名稱:

21:56:55.074 | DEBUG | alice | http-bio-8080-exec-9 | ... //... 21:56:57.314 | DEBUG | bob (alice) | http-bio-8080-exec-3 | ...

第二個日志語句很有趣。 如果您看一下上面的findSwitchedUser()調用,很明顯,作為管理員的alice切換到用戶bob ,現在代表他瀏覽。

有時您需要更強大的審核系統。 幸運的是,Spring框架具有內置的事件基礎結構,我們可以利用在有人切換用戶并退出此模式時發送的AuthenticationSwitchUserEvent優勢:

@Service public class SwitchUserListenerimplements ApplicationListener<AuthenticationSwitchUserEvent> {private static final Logger log = LoggerFactory.getLogger(SwitchUserListener.class);@Overridepublic void onApplicationEvent(AuthenticationSwitchUserEvent event) {log.info("User switch from {} to {}",event.getAuthentication().getName(),event.getTargetUser().getUsername());} }

當然,您可以使用所需的任何業務邏輯來替換簡單的日志記錄,例如,將此類事件存儲在數據庫中或向安全員發送電子郵件。

因此,我們知道如何以其他用戶身份登錄一段時間,然后退出這種模式。 但是,如果我們需要“ sudo ”,即代表其他用戶僅發出一個HTTP請求,該怎么辦? 當然,我們可以切換到該用戶,運行該請求,然后退出。 但這似乎太繁瑣且麻煩。 當客戶端程序訪問我們的API并希望以其他用戶身份查看數據時(考慮測試復雜的ACL),可能會彈出這樣的要求。

添加自定義HTTP標頭以表示這樣的特殊模擬請求聽起來很合理。 假設客戶端已經在進行身份驗證(例如使用JSESSIONID cookie),則它僅在一個請求期間有效。 不幸的是,Spring Security不支持此功能,但是很容易在SwitchUserFilter之上SwitchUserFilter :

public class SwitchUserOnceFilter extends SwitchUserFilter {@Overridepublic void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) req;final String switchUserHeader = request.getHeader("X-Switch-User-Once");if (switchUserHeader != null) {trySwitchingUserForThisRequest(chain, request, res, switchUserHeader);} else {super.doFilter(req, res, chain);}}private void trySwitchingUserForThisRequest(FilterChain chain, HttpServletRequest request, ServletResponse response, String switchUserHeader) throws IOException, ServletException {try {proceedWithSwitchedUser(chain, request, response, switchUserHeader);} catch (AuthenticationException e) {throw Throwables.propagate(e);}}private void proceedWithSwitchedUser(FilterChain chain, HttpServletRequest request, ServletResponse response, String switchUserHeader) throws IOException, ServletException {final Authentication targetUser = attemptSwitchUser(new SwitchUserRequest(request, switchUserHeader));SecurityContextHolder.getContext().setAuthentication(targetUser);try {chain.doFilter(request, response);} finally {final Authentication originalUser = attemptExitUser(request);SecurityContextHolder.getContext().setAuthentication(originalUser);}}}

與原始SwitchUserFilter的唯一區別是,如果存在"X-Switch-User-Once" ,我們將憑據切換到由此標頭的值表示的用戶-但是僅在一個HTTP請求期間。 SwitchUserFilter假定要切換到的用戶名在j_username參數下,因此我不得不使用SwitchUserRequest包裝器作弊:

private class SwitchUserRequest extends HttpServletRequestWrapper {private final String switchUserHeader;public SwitchUserRequest(HttpServletRequest request, String switchUserHeader) {super(request);this.switchUserHeader = switchUserHeader;}@Overridepublic String getParameter(String name) {switch (name) {case SPRING_SECURITY_SWITCH_USERNAME_KEY:return switchUserHeader;default:return super.getParameter(name);}} }

我們的自定義“ sudo ”就位了! 您可以使用curl進行測試:

$ curl localhost:8080/books/rest/book \-H "X-Switch-User-Once: bob" \-b "JSESSIONID=..."

當然,如果沒有JSESSIONID cookie,系統將不允許我們進入。我們必須首先登錄,并具有訪問sudo功能的特殊特權。 切換用戶是一個方便且功能強大的工具。 如果您想在實踐中嘗試,請在GitHub上查看工作示例 。

參考: Java和社區博客中JCG合作伙伴 Tomasz Nurkiewicz提供的Spring Security應用程序中的su和sudo 。

翻譯自: https://www.javacodegeeks.com/2013/07/su-and-sudo-in-spring-security-applications.html

sudo su su

總結

以上是生活随笔為你收集整理的sudo su su_Spring Security应用程序中的su和sudo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。