Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save attacomsian/238a4f21c9c98b57806f566f1dbcb7e7 to your computer and use it in GitHub Desktop.
Save attacomsian/238a4f21c9c98b57806f566f1dbcb7e7 to your computer and use it in GitHub Desktop.

Revisions

  1. @jecyhw jecyhw revised this gist Dec 29, 2016. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions AjaxAwareAuthenticationEntryPoint.java
    Original 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';
    }
    });
    **/
  2. @jecyhw jecyhw revised this gist Dec 28, 2016. 1 changed file with 20 additions and 1 deletion.
    21 changes: 20 additions & 1 deletion AjaxAwareAuthenticationEntryPoint.java
    Original 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"));
    }
    }
    }
    **/
  3. @jecyhw jecyhw revised this gist Dec 28, 2016. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions AjaxAwareAuthenticationEntryPoint.java
    Original 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"));
    }
  4. @jecyhw jecyhw created this gist Dec 28, 2016.
    15 changes: 15 additions & 0 deletions AjaxAwareAuthenticationEntryPoint.java
    Original 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);
    }
    }
    }