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 );
2818
2919 @ Override
3020 protected void configure (HttpSecurity http ) throws Exception {
31- RequestCache requestCache = refererRequestCache ();
32- SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler ();
33- handler .setRequestCache (requestCache );
3421 http
35- .exceptionHandling ()
36- .authenticationEntryPoint (new LoginUrlAuthenticationEntryPoint ("/oauth2/authorization/okta" ))
37- .and ()
38- .oauth2Login ()
39- .successHandler (handler )
40- .and ()
22+ .oauth2Login ().and ()
4123 .csrf ()
4224 .csrfTokenRepository (CookieCsrfTokenRepository .withHttpOnlyFalse ())
4325 .and ()
44- .requestCache ()
45- .requestCache (requestCache )
46- .and ()
4726 .authorizeRequests ()
4827 .antMatchers ("/**/*.{js,html,css}" ).permitAll ()
4928 .antMatchers ("/" , "/api/user" ).permitAll ()
5029 .anyRequest ().authenticated ();
5130 }
5231
5332 @ Bean
33+ @ Profile ("dev" )
5434 public RequestCache refererRequestCache () {
55- return new RequestCache () {
56- private String savedAttrName = getClass ().getName ().concat (".SAVED" );
57-
35+ return new HttpSessionRequestCache () {
5836 @ Override
5937 public void saveRequest (HttpServletRequest request , HttpServletResponse response ) {
6038 String referrer = request .getHeader ("referer" );
6139 if (referrer != null ) {
62- request .getSession ().setAttribute (this .savedAttrName , referrerRequest (referrer ));
63- }
64- }
65-
66- @ Override
67- public SavedRequest getRequest (HttpServletRequest request , HttpServletResponse response ) {
68- HttpSession session = request .getSession (false );
69-
70- if (session != null ) {
71- return (SavedRequest ) session .getAttribute (this .savedAttrName );
72- }
73-
74- return null ;
75- }
76-
77- @ Override
78- public HttpServletRequest getMatchingRequest (HttpServletRequest request , HttpServletResponse response ) {
79- return request ;
80- }
81-
82- @ Override
83- public void removeRequest (HttpServletRequest request , HttpServletResponse response ) {
84- HttpSession session = request .getSession (false );
85-
86- if (session != null ) {
87- log .debug ("Removing SavedRequest from session if present" );
88- session .removeAttribute (this .savedAttrName );
40+ request .getSession ().setAttribute ("SPRING_SECURITY_SAVED_REQUEST" , new SimpleSavedRequest (referrer ));
8941 }
9042 }
9143 };
9244 }
93-
94- private SavedRequest referrerRequest (final String referrer ) {
95- return new SavedRequest () {
96- @ Override
97- public String getRedirectUrl () {
98- return referrer ;
99- }
100-
101- @ Override
102- public List <Cookie > getCookies () {
103- return null ;
104- }
105-
106- @ Override
107- public String getMethod () {
108- return null ;
109- }
110-
111- @ Override
112- public List <String > getHeaderValues (String name ) {
113- return null ;
114- }
115-
116- @ Override
117- public Collection <String > getHeaderNames () {
118- return null ;
119- }
120-
121- @ Override
122- public List <Locale > getLocales () {
123- return null ;
124- }
125-
126- @ Override
127- public String [] getParameterValues (String name ) {
128- return new String [0 ];
129- }
130-
131- @ Override
132- public Map <String , String []> getParameterMap () {
133- return null ;
134- }
135- };
136- }
13745}
0 commit comments