11package com .okta .developer .jugtours .config ;
22
3- import org .slf4j .Logger ;
4- import org .slf4j .LoggerFactory ;
53import org .springframework .context .annotation .Bean ;
64import org .springframework .context .annotation .Configuration ;
75import org .springframework .context .annotation .Profile ;
86import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
97import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
10- import org .springframework .security .web .authentication .LoginUrlAuthenticationEntryPoint ;
11- import org .springframework .security .web .authentication .SavedRequestAwareAuthenticationSuccessHandler ;
128import org .springframework .security .web .csrf .CookieCsrfTokenRepository ;
9+ import org .springframework .security .web .savedrequest .HttpSessionRequestCache ;
1310import org .springframework .security .web .savedrequest .RequestCache ;
14- import org .springframework .security .web .savedrequest .SavedRequest ;
11+ import org .springframework .security .web .savedrequest .SimpleSavedRequest ;
1512
16- import javax .servlet .http .Cookie ;
1713import javax .servlet .http .HttpServletRequest ;
1814import javax .servlet .http .HttpServletResponse ;
19- import javax .servlet .http .HttpSession ;
20- import java .util .Collection ;
21- import java .util .List ;
22- import java .util .Locale ;
23- import java .util .Map ;
2415
2516@ Configuration
2617public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
27- private final Logger log = LoggerFactory .getLogger (SecurityConfiguration .class );
28-
2918 @ Override
3019 protected void configure (HttpSecurity http ) throws Exception {
31- RequestCache requestCache = refererRequestCache ();
32- SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler ();
33- handler .setRequestCache (requestCache );
3420 http
35- .oauth2Login ()
36- .successHandler (handler )
37- .and ()
21+ .oauth2Login ().and ()
3822 .csrf ()
3923 .csrfTokenRepository (CookieCsrfTokenRepository .withHttpOnlyFalse ())
4024 .and ()
41- .requestCache ()
42- .requestCache (requestCache )
43- .and ()
4425 .authorizeRequests ()
4526 .antMatchers ("/**/*.{js,html,css}" ).permitAll ()
4627 .antMatchers ("/" , "/api/user" ).permitAll ()
4728 .anyRequest ().authenticated ();
4829 }
4930
5031 @ Bean
32+ @ Profile ("dev" )
5133 public RequestCache refererRequestCache () {
52- return new RequestCache () {
53- private String savedAttrName = getClass ().getName ().concat (".SAVED" );
54-
34+ return new HttpSessionRequestCache () {
5535 @ Override
5636 public void saveRequest (HttpServletRequest request , HttpServletResponse response ) {
5737 String referrer = request .getHeader ("referer" );
5838 if (referrer != null ) {
59- request .getSession ().setAttribute (this .savedAttrName , referrerRequest (referrer ));
60- }
61- }
62-
63- @ Override
64- public SavedRequest getRequest (HttpServletRequest request , HttpServletResponse response ) {
65- HttpSession session = request .getSession (false );
66-
67- if (session != null ) {
68- return (SavedRequest ) session .getAttribute (this .savedAttrName );
69- }
70-
71- return null ;
72- }
73-
74- @ Override
75- public HttpServletRequest getMatchingRequest (HttpServletRequest request , HttpServletResponse response ) {
76- return request ;
77- }
78-
79- @ Override
80- public void removeRequest (HttpServletRequest request , HttpServletResponse response ) {
81- HttpSession session = request .getSession (false );
82-
83- if (session != null ) {
84- log .debug ("Removing SavedRequest from session if present" );
85- session .removeAttribute (this .savedAttrName );
39+ request .getSession ().setAttribute ("SPRING_SECURITY_SAVED_REQUEST" , new SimpleSavedRequest (referrer ));
8640 }
8741 }
8842 };
8943 }
90-
91- private SavedRequest referrerRequest (final String referrer ) {
92- return new SavedRequest () {
93- @ Override
94- public String getRedirectUrl () {
95- return referrer ;
96- }
97-
98- @ Override
99- public List <Cookie > getCookies () {
100- return null ;
101- }
102-
103- @ Override
104- public String getMethod () {
105- return null ;
106- }
107-
108- @ Override
109- public List <String > getHeaderValues (String name ) {
110- return null ;
111- }
112-
113- @ Override
114- public Collection <String > getHeaderNames () {
115- return null ;
116- }
117-
118- @ Override
119- public List <Locale > getLocales () {
120- return null ;
121- }
122-
123- @ Override
124- public String [] getParameterValues (String name ) {
125- return new String [0 ];
126- }
127-
128- @ Override
129- public Map <String , String []> getParameterMap () {
130- return null ;
131- }
132- };
133- }
13444}
0 commit comments