11package com .okta .developer .jugtours .config ;
22
3+ import org .slf4j .Logger ;
4+ import org .slf4j .LoggerFactory ;
35import org .springframework .boot .autoconfigure .security .oauth2 .client .EnableOAuth2Sso ;
6+ import org .springframework .context .annotation .Bean ;
47import org .springframework .context .annotation .Configuration ;
8+ import org .springframework .context .annotation .Profile ;
59import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
610import org .springframework .security .config .annotation .web .builders .WebSecurity ;
711import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
12+ import org .springframework .security .web .authentication .SavedRequestAwareAuthenticationSuccessHandler ;
813import org .springframework .security .web .csrf .CookieCsrfTokenRepository ;
14+ import org .springframework .security .web .savedrequest .RequestCache ;
15+ import org .springframework .security .web .savedrequest .SavedRequest ;
16+
17+ import javax .servlet .http .Cookie ;
18+ import javax .servlet .http .HttpServletRequest ;
19+ import javax .servlet .http .HttpServletResponse ;
20+ import javax .servlet .http .HttpSession ;
21+ import java .util .Collection ;
22+ import java .util .List ;
23+ import java .util .Locale ;
24+ import java .util .Map ;
925
1026@ Configuration
1127@ EnableOAuth2Sso
1228public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
29+ private final Logger log = LoggerFactory .getLogger (SecurityConfiguration .class );
1330
1431 @ Override
1532 public void configure (WebSecurity web ) throws Exception {
@@ -18,15 +35,108 @@ public void configure(WebSecurity web) throws Exception {
1835
1936 @ Override
2037 protected void configure (HttpSecurity http ) throws Exception {
38+ RequestCache requestCache = refererRequestCache ();
39+ SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler ();
40+ handler .setRequestCache (requestCache );
2141 http
2242 .csrf ().csrfTokenRepository (CookieCsrfTokenRepository .withHttpOnlyFalse ())
43+ .and ()
44+ .oauth2Login ()
45+ .successHandler (handler )
2346 .and ()
2447 .authorizeRequests ()
48+ .antMatchers ("/**/*.{js,html,css}" ).permitAll ()
2549 .antMatchers ("/" , "/api/user" ).permitAll ()
2650 .anyRequest ().authenticated ();/*
2751 .and()
2852 .requiresChannel()
2953 .requestMatchers(r -> r.getHeader("x-forwarded-proto") != null)
3054 .requiresSecure();*/
3155 }
56+
57+ @ Bean
58+ @ Profile ("dev" )
59+ public RequestCache refererRequestCache () {
60+ return new RequestCache () {
61+ private String savedAttrName = getClass ().getName ().concat (".SAVED" );
62+
63+ @ Override
64+ public void saveRequest (HttpServletRequest request , HttpServletResponse response ) {
65+ String referrer = request .getHeader ("referer" );
66+ if (referrer != null ) {
67+ request .getSession ().setAttribute (this .savedAttrName , referrerRequest (referrer ));
68+ }
69+ }
70+
71+ @ Override
72+ public SavedRequest getRequest (HttpServletRequest request , HttpServletResponse response ) {
73+ HttpSession session = request .getSession (false );
74+
75+ if (session != null ) {
76+ return (SavedRequest ) session .getAttribute (this .savedAttrName );
77+ }
78+
79+ return null ;
80+ }
81+
82+ @ Override
83+ public HttpServletRequest getMatchingRequest (HttpServletRequest request , HttpServletResponse response ) {
84+ return request ;
85+ }
86+
87+ @ Override
88+ public void removeRequest (HttpServletRequest request , HttpServletResponse response ) {
89+ HttpSession session = request .getSession (false );
90+
91+ if (session != null ) {
92+ log .debug ("Removing SavedRequest from session if present" );
93+ session .removeAttribute (this .savedAttrName );
94+ }
95+ }
96+ };
97+ }
98+
99+ private SavedRequest referrerRequest (final String referrer ) {
100+ return new SavedRequest () {
101+ @ Override
102+ public String getRedirectUrl () {
103+ return referrer ;
104+ }
105+
106+ @ Override
107+ public List <Cookie > getCookies () {
108+ return null ;
109+ }
110+
111+ @ Override
112+ public String getMethod () {
113+ return null ;
114+ }
115+
116+ @ Override
117+ public List <String > getHeaderValues (String name ) {
118+ return null ;
119+ }
120+
121+ @ Override
122+ public Collection <String > getHeaderNames () {
123+ return null ;
124+ }
125+
126+ @ Override
127+ public List <Locale > getLocales () {
128+ return null ;
129+ }
130+
131+ @ Override
132+ public String [] getParameterValues (String name ) {
133+ return new String [0 ];
134+ }
135+
136+ @ Override
137+ public Map <String , String []> getParameterMap () {
138+ return null ;
139+ }
140+ };
141+ }
32142}
0 commit comments