23
23
import org .springframework .context .annotation .Configuration ;
24
24
import org .springframework .security .core .Authentication ;
25
25
import org .springframework .security .core .context .SecurityContextHolder ;
26
- import org .springframework .util .CollectionUtils ;
27
26
import org .springframework .web .context .request .RequestContextHolder ;
28
27
import org .springframework .web .context .request .ServletRequestAttributes ;
29
28
import reactor .core .CoreSubscriber ;
@@ -70,7 +69,7 @@ public void afterPropertiesSet() throws Exception {
70
69
Operators .liftPublisher ((pub , sub ) -> createSubscriberIfNecessary (sub ));
71
70
72
71
Hooks .onLastOperator (SECURITY_REACTOR_CONTEXT_OPERATOR_KEY , pub -> {
73
- if (CollectionUtils . isEmpty ( getContextAttributes () )) {
72
+ if (! contextAttributesAvailable ( )) {
74
73
// No need to decorate so return original Publisher
75
74
return pub ;
76
75
}
@@ -91,6 +90,22 @@ <T> CoreSubscriber<T> createSubscriberIfNecessary(CoreSubscriber<T> delegate) {
91
90
return new SecurityReactorContextSubscriber <>(delegate , getContextAttributes ());
92
91
}
93
92
93
+ private static boolean contextAttributesAvailable () {
94
+ HttpServletRequest servletRequest = null ;
95
+ HttpServletResponse servletResponse = null ;
96
+ ServletRequestAttributes requestAttributes =
97
+ (ServletRequestAttributes ) RequestContextHolder .getRequestAttributes ();
98
+ if (requestAttributes != null ) {
99
+ servletRequest = requestAttributes .getRequest ();
100
+ servletResponse = requestAttributes .getResponse ();
101
+ }
102
+ Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
103
+ if (authentication != null || servletRequest != null || servletResponse != null ) {
104
+ return true ;
105
+ }
106
+ return false ;
107
+ }
108
+
94
109
private static Map <Object , Object > getContextAttributes () {
95
110
HttpServletRequest servletRequest = null ;
96
111
HttpServletResponse servletResponse = null ;
0 commit comments