Last active
August 29, 2015 14:13
-
-
Save phuonghuynh/857adec21c1cfd66b22a to your computer and use it in GitHub Desktop.
Spring Security 4x support Web Socket
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 characters
public class HttpSessionInitializer extends AbstractHttpSessionApplicationInitializer { | |
protected String getDispatcherWebApplicationContextSuffix() { | |
return AbstractDispatcherServletInitializer.DEFAULT_SERVLET_NAME; | |
} | |
} |
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 characters
@EnableWebSecurity | |
@EnableGlobalMethodSecurity(prePostEnabled = true) | |
@EnableRedisHttpSession | |
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { | |
@Bean | |
public EmbeddedRedisServer redisServer() { | |
return new EmbeddedRedisServer(); | |
} | |
@Bean | |
public JedisConnectionFactory connectionFactory() throws Exception { | |
return new JedisConnectionFactory(); | |
} | |
@Bean | |
public AuthenticationManager authenticationManager() { | |
return new SocialAuthenticationManager(); | |
} | |
protected void configure(HttpSecurity http) throws Exception { | |
http.csrf().disable() | |
.authorizeRequests().antMatchers("/user/**").hasAuthority("USER") | |
.and().formLogin().loginPage("/login").usernameParameter("key").successHandler(getSuccessHandler()).failureHandler(getAuthenticationFailureHandler()) | |
.and().logout().logoutUrl("/logout").logoutSuccessHandler(getLogoutSuccessHandler()).invalidateHttpSession(true).deleteCookies("SESSION").permitAll() | |
.and().exceptionHandling().authenticationEntryPoint(getAuthenticationEntryPoint()); | |
} | |
private AuthenticationEntryPoint getAuthenticationEntryPoint() { | |
return (request, response, authException) -> { | |
response.setStatus(HttpServletResponse.SC_FORBIDDEN); | |
}; | |
} | |
private LogoutSuccessHandler getLogoutSuccessHandler() { | |
return (request, response, authentication) -> { | |
response.setStatus(HttpServletResponse.SC_NO_CONTENT); | |
}; | |
} | |
private AuthenticationFailureHandler getAuthenticationFailureHandler() { | |
return (request, response, exception) -> { | |
response.setStatus(HttpServletResponse.SC_FORBIDDEN); | |
}; | |
} | |
private AuthenticationSuccessHandler getSuccessHandler() { | |
return (request, response, authentication) -> { | |
response.setStatus(HttpServletResponse.SC_NO_CONTENT); | |
}; | |
} | |
public void configure(WebSecurity web) throws Exception { | |
web.ignoring().antMatchers("/images/**", "/css/**", "/generate-resources/**", "/modules/**", "/bower_components/**", "/custom-js/**"); | |
} | |
} |
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 characters
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { | |
protected String getDispatcherWebApplicationContextSuffix() { | |
return AbstractDispatcherServletInitializer.DEFAULT_SERVLET_NAME; | |
} | |
} |
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 characters
public class WebSocketSecurityConfiguration extends AbstractSecurityWebSocketMessageBrokerConfigurer { | |
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) { | |
messages | |
.antMatchers("/user/**", "/queue/**", "/app/user/**").hasAuthority("USER"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Most Helpful. Thanks.