11/*
2- * Copyright 2002-2020 the original author or authors.
2+ * Copyright 2002-2022 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2121import org .springframework .security .authentication .AuthenticationTrustResolver ;
2222import org .springframework .security .authentication .AuthenticationTrustResolverImpl ;
2323import org .springframework .security .core .Authentication ;
24+ import org .springframework .util .Assert ;
2425
2526/**
2627 * An {@link AuthorizationManager} that determines if the current user is authenticated.
3132 */
3233public final class AuthenticatedAuthorizationManager <T > implements AuthorizationManager <T > {
3334
34- private final AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl ();
35+ private final AbstractAuthorizationStrategy authorizationStrategy ;
36+
37+ /**
38+ * Creates an instance that determines if the current user is authenticated, this is
39+ * the same as calling {@link #authenticated()} factory method.
40+ *
41+ * @since 5.8
42+ * @see #authenticated()
43+ * @see #fullyAuthenticated()
44+ * @see #rememberMe()
45+ * @see #anonymous()
46+ */
47+ public AuthenticatedAuthorizationManager () {
48+ this (new AuthenticatedAuthorizationStrategy ());
49+ }
50+
51+ private AuthenticatedAuthorizationManager (AbstractAuthorizationStrategy authorizationStrategy ) {
52+ this .authorizationStrategy = authorizationStrategy ;
53+ }
54+
55+ /**
56+ * Sets the {@link AuthenticationTrustResolver} to be used. Default is
57+ * {@link AuthenticationTrustResolverImpl}. Cannot be null.
58+ * @param trustResolver the {@link AuthenticationTrustResolver} to use
59+ * @since 5.8
60+ */
61+ public void setTrustResolver (AuthenticationTrustResolver trustResolver ) {
62+ this .authorizationStrategy .setTrustResolver (trustResolver );
63+ }
3564
3665 /**
3766 * Creates an instance of {@link AuthenticatedAuthorizationManager}.
@@ -43,24 +72,98 @@ public static <T> AuthenticatedAuthorizationManager<T> authenticated() {
4372 }
4473
4574 /**
46- * Determines if the current user is authorized by evaluating if the
47- * {@link Authentication} is not anonymous and authenticated.
75+ * Creates an instance of {@link AuthenticatedAuthorizationManager} that determines if
76+ * the {@link Authentication} is authenticated without using remember me.
77+ * @param <T> the type of object being authorized
78+ * @return the new instance
79+ * @since 5.8
80+ */
81+ public static <T > AuthenticatedAuthorizationManager <T > fullyAuthenticated () {
82+ return new AuthenticatedAuthorizationManager <>(new FullyAuthenticatedAuthorizationStrategy ());
83+ }
84+
85+ /**
86+ * Creates an instance of {@link AuthenticatedAuthorizationManager} that determines if
87+ * the {@link Authentication} is authenticated using remember me.
88+ * @param <T> the type of object being authorized
89+ * @return the new instance
90+ * @since 5.8
91+ */
92+ public static <T > AuthenticatedAuthorizationManager <T > rememberMe () {
93+ return new AuthenticatedAuthorizationManager <>(new RememberMeAuthorizationStrategy ());
94+ }
95+
96+ /**
97+ * Creates an instance of {@link AuthenticatedAuthorizationManager} that determines if
98+ * the {@link Authentication} is anonymous.
99+ * @param <T> the type of object being authorized
100+ * @return the new instance
101+ * @since 5.8
102+ */
103+ public static <T > AuthenticatedAuthorizationManager <T > anonymous () {
104+ return new AuthenticatedAuthorizationManager <>(new AnonymousAuthorizationStrategy ());
105+ }
106+
107+ /**
108+ * Determines if the current user is authorized according to the given strategy.
48109 * @param authentication the {@link Supplier} of the {@link Authentication} to check
49110 * @param object the {@link T} object to check
50111 * @return an {@link AuthorizationDecision}
51112 */
52113 @ Override
53114 public AuthorizationDecision check (Supplier <Authentication > authentication , T object ) {
54- boolean granted = isGranted (authentication .get ());
115+ boolean granted = this . authorizationStrategy . isGranted (authentication .get ());
55116 return new AuthorizationDecision (granted );
56117 }
57118
58- private boolean isGranted (Authentication authentication ) {
59- return authentication != null && isNotAnonymous (authentication ) && authentication .isAuthenticated ();
119+ private abstract static class AbstractAuthorizationStrategy {
120+
121+ AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl ();
122+
123+ private void setTrustResolver (AuthenticationTrustResolver trustResolver ) {
124+ Assert .notNull (trustResolver , "trustResolver cannot be null" );
125+ this .trustResolver = trustResolver ;
126+ }
127+
128+ abstract boolean isGranted (Authentication authentication );
129+
60130 }
61131
62- private boolean isNotAnonymous (Authentication authentication ) {
63- return !this .trustResolver .isAnonymous (authentication );
132+ private static class AuthenticatedAuthorizationStrategy extends AbstractAuthorizationStrategy {
133+
134+ @ Override
135+ boolean isGranted (Authentication authentication ) {
136+ return authentication != null && !this .trustResolver .isAnonymous (authentication )
137+ && authentication .isAuthenticated ();
138+ }
139+
140+ }
141+
142+ private static final class FullyAuthenticatedAuthorizationStrategy extends AuthenticatedAuthorizationStrategy {
143+
144+ @ Override
145+ boolean isGranted (Authentication authentication ) {
146+ return super .isGranted (authentication ) && !this .trustResolver .isRememberMe (authentication );
147+ }
148+
149+ }
150+
151+ private static final class AnonymousAuthorizationStrategy extends AbstractAuthorizationStrategy {
152+
153+ @ Override
154+ boolean isGranted (Authentication authentication ) {
155+ return this .trustResolver .isAnonymous (authentication );
156+ }
157+
158+ }
159+
160+ private static final class RememberMeAuthorizationStrategy extends AbstractAuthorizationStrategy {
161+
162+ @ Override
163+ boolean isGranted (Authentication authentication ) {
164+ return this .trustResolver .isRememberMe (authentication );
165+ }
166+
64167 }
65168
66169}
0 commit comments