Spring Security bietet JSP tag support für diese. Zum Beispiel:
<sec:authorize url="/admin">
This content will only be visible to users who are authorized to access the "/admin" URL.
</sec:authorize>
Thymeleaf bietet Spring Security Dialect für checking URL authorization mit Spring Security direkte Unterstützung hat. Zum Beispiel:
<div sec:authorize-url="/admin">
This will only be displayed if authenticated user can call the "/admin" URL.
</div>
Wenn Ihre Technologie nicht auf der Bühne den Scheck direkt unterstützt, können Sie bequem die WebInvocationPrivilegeEvaluator verwenden (dies ist das Objekt, das die JSP taglib und Thymeleaf Gebrauch). Zum Beispiel können Sie @Autowire
eine Instanz von WebInvocationPrivilegeEvaluator
und verwenden Sie es direkt. Offensichtlich hängt die Syntax davon ab, wo Sie sie verwenden (d. H. GSP, Freemarker usw.), aber hier ist ein Beispiel in reinem Java-Code.
@Autowired
WebInvocationPrivilegeEvaluator webPrivs;
public void useIt() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
boolean hasAdminAccess = webPrivs.isAllowed("/admin", authentication);
boolean hasAdminPostAccess = webPrivs.isAllowed(null, "/admin", "POST", authentication);
}
Sie meine Anfrage einschließlich Intercept-URL-Muster oder es ist ssl authentifiziert? – HRgiger
Ich meine Intercept-URL-Muster und sichere Annotation. – user1641877
Ich möchte testen, ob die aktuelle Anfrage, in der ich mich befinde, entweder anonym ist oder als gesichert markiert wurde, entweder mit einer Config oder mit @Secured Annotation – user1641877