Forked from jecyhw/AjaxAwareAuthenticationEntryPoint.java
Created
November 18, 2020 10:04
-
-
Save attacomsian/238a4f21c9c98b57806f566f1dbcb7e7 to your computer and use it in GitHub Desktop.
Revisions
-
jecyhw revised this gist
Dec 29, 2016 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,4 +55,12 @@ protected void configure(HttpSecurity http) throws Exception { .and().exceptionHandling().authenticationEntryPoint(new AjaxAwareAuthenticationEntryPoint("/login")); } } **/ /** $(document).ajaxError(function (e, xhr, options) { if (xhr.status == 403) { window.location.href = 'login'; } }); **/ -
jecyhw revised this gist
Dec 28, 2016 . 1 changed file with 20 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,6 +14,23 @@ public void commence(HttpServletRequest request, HttpServletResponse response, A } } /** @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Bean public BCryptPasswordEncoder bCryptPasswordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder()); } @Override protected void configure(HttpSecurity http) throws Exception { http @@ -36,4 +53,6 @@ protected void configure(HttpSecurity http) throws Exception { .logout().logoutSuccessUrl("/login").logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll() .and().csrf() .and().exceptionHandling().authenticationEntryPoint(new AjaxAwareAuthenticationEntryPoint("/login")); } } **/ -
jecyhw revised this gist
Dec 28, 2016 . 1 changed file with 24 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,3 +13,27 @@ public void commence(HttpServletRequest request, HttpServletResponse response, A } } } @Override protected void configure(HttpSecurity http) throws Exception { http .headers() .frameOptions().sameOrigin() .and() .authorizeRequests() .antMatchers("/**/create", "/**/recognition", "/**/delete*", "/**/uploadFile*").authenticated() .antMatchers("/**").permitAll() .and() .formLogin() .loginPage("/login?auth") .loginProcessingUrl("/login") .failureUrl("/login?error") .defaultSuccessUrl("/") .usernameParameter("userName") .passwordParameter("password") .permitAll() .and() .logout().logoutSuccessUrl("/login").logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll() .and().csrf() .and().exceptionHandling().authenticationEntryPoint(new AjaxAwareAuthenticationEntryPoint("/login")); } -
jecyhw created this gist
Dec 28, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ public class AjaxAwareAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint { public AjaxAwareAuthenticationEntryPoint(String loginFormUrl) { super(loginFormUrl); } @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { String ajaxHeader = ((HttpServletRequest) request).getHeader("X-Requested-With"); if ("XMLHttpRequest".equals(ajaxHeader)) { response.sendError(HttpServletResponse.SC_FORBIDDEN, "Ajax Request Denied (Session Expired)"); } else { super.commence(request, response, authException); } } }