A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
7.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package org.ros.chatto;
  2. import org.ros.chatto.logged.MyLogoutSuccessHandler;
  3. import org.ros.chatto.logged.MySimpleUrlAuthenticationSuccessHandler;
  4. import org.ros.chatto.security.CustomBasicAuthenticationFilter;
  5. import org.ros.chatto.security.MyUserDetailsService;
  6. import org.ros.chatto.security.TokenAuthenticationFilter;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.core.annotation.Order;
  11. import org.springframework.security.authentication.AuthenticationManager;
  12. import org.springframework.security.authentication.AuthenticationProvider;
  13. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  14. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  15. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  16. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  17. import org.springframework.security.config.http.SessionCreationPolicy;
  18. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  19. import org.springframework.security.crypto.password.PasswordEncoder;
  20. import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
  21. import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
  22. @Configuration
  23. @EnableWebSecurity
  24. public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
  25. @Autowired
  26. private AuthenticationSuccessHandler authenticationSuccessHandler;
  27. @Autowired
  28. private MyUserDetailsService myUserDetailsService;
  29. @Autowired
  30. private PasswordEncoder passwordEncoder;
  31. @Override
  32. @Bean
  33. public AuthenticationManager authenticationManagerBean() throws Exception {
  34. return super.authenticationManagerBean();
  35. }
  36. @Bean
  37. public AuthenticationProvider authenticationProvider() {
  38. DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
  39. provider.setUserDetailsService(myUserDetailsService);
  40. provider.setPasswordEncoder(passwordEncoder);
  41. return provider;
  42. }
  43. @Bean
  44. public static PasswordEncoder passwordEncoder() {
  45. return new BCryptPasswordEncoder();
  46. }
  47. @Configuration
  48. @Order(1)
  49. public static class ApiWebSecurity extends WebSecurityConfigurerAdapter {
  50. @Autowired
  51. private RESTAuthenticationEntryPoint authenticationEntryPoint;
  52. @Autowired
  53. private CustomBasicAuthenticationFilter customBasicAuthFilter;
  54. @Autowired
  55. private TokenAuthenticationFilter tokenFilter;
  56. @Override
  57. protected void configure(HttpSecurity http) throws Exception {
  58. http.csrf().disable().exceptionHandling()
  59. .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
  60. // .cors().and()
  61. .antMatcher("/api/**").authorizeRequests()
  62. .antMatchers("/api/chat/**").hasAnyRole("USER", "ADMIN", "SUPER_USER")
  63. .antMatchers("/api/demo/**").hasRole("SUPER_USER")
  64. // .antMatchers("/perform-login").permitAll()
  65. .anyRequest()
  66. // .hasAnyRole("USER", "ADMIN", "SUPER_USER")
  67. .authenticated()
  68. .and().httpBasic().authenticationEntryPoint(authenticationEntryPoint)
  69. // .and()
  70. // .logout().invalidateHttpSession(true).clearAuthentication(true)
  71. // .logoutRequestMatcher(new AntPathRequestMatcher("/api/perform_logout"))
  72. // .logoutSuccessUrl("/").permitAll()
  73. // .and()
  74. // .formLogin()
  75. // .loginProcessingUrl("/api/perform_login").permitAll()
  76. // .and()
  77. // .formLogin()
  78. // .and()
  79. // .logout();
  80. ;
  81. http.addFilterBefore(tokenFilter, BasicAuthenticationFilter.class);
  82. // Creating token when basic authentication is successful and the same token can
  83. // be used to authenticate for further requests
  84. // final CustomBasicAuthenticationFilter customBasicAuthFilter = new CustomBasicAuthenticationFilter(
  85. // authenticationManagerBean());
  86. http.addFilter(customBasicAuthFilter);
  87. }
  88. // @Override
  89. // protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  90. // auth.eraseCredentials(false);
  91. // }
  92. //
  93. // public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
  94. //
  95. // }
  96. }
  97. @Configuration
  98. @Order(2)
  99. public static class FormWebSecurity extends WebSecurityConfigurerAdapter {
  100. @Autowired
  101. private MySimpleUrlAuthenticationSuccessHandler mySimpleUrlAuthenticationSuccessHandler;
  102. @Autowired
  103. private MyLogoutSuccessHandler myLogoutSuccessHandler;
  104. @Override
  105. protected void configure(HttpSecurity httpSecurity) throws Exception {
  106. httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS).and()
  107. .authorizeRequests()
  108. // .antMatchers(HttpMethod.POST, "/api/**").permitAll()
  109. .antMatchers("/", "perform_login", "/logout**", "/favicon.ico", "/login*", "/registration",
  110. "/perform_registration", "/css/**", "/js/**", "/img/**")
  111. .permitAll()
  112. // .antMatchers("/","/api**","/api/**","/login*","/registration","/perform_registration","/css/**", "/js/**", "/images/**").permitAll()
  113. .antMatchers("/user/**").hasAnyRole("USER", "ADMIN", "SUPER_USER").antMatchers("/admin/**")
  114. .hasAnyRole("ADMIN", "SUPER_USER")
  115. // .and()
  116. // .antMatcher("/api/**")
  117. // .authorizeRequests()
  118. .anyRequest().authenticated()
  119. .and()
  120. .formLogin().loginPage("/login").permitAll().loginProcessingUrl("/perform_login")
  121. .successHandler(mySimpleUrlAuthenticationSuccessHandler).and().logout()
  122. .logoutSuccessHandler(myLogoutSuccessHandler)
  123. // .failureUrl("/?login_error")
  124. // .and()
  125. // .logout().invalidateHttpSession(true)
  126. // .clearAuthentication(true)
  127. // .logoutRequestMatcher(new AntPathRequestMatcher("/perform_logout"))
  128. // .logoutSuccessUrl("/").permitAll()
  129. // .and().httpBasic();
  130. // .and().cors()
  131. // .and().csrf().disable();
  132. ;
  133. // httpSecurity
  134. // .csrf().disable()
  135. // .authorizeRequests().antMatchers("login").permitAll()
  136. // .anyRequest().authenticated()
  137. // .and()
  138. // .formLogin()
  139. // .loginPage("/login").permitAll()
  140. // .and()
  141. // .logout().invalidateHttpSession(true)
  142. // .clearAuthentication(true)
  143. // .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
  144. // .logoutSuccessUrl("/").permitAll();
  145. }
  146. // @Override
  147. // protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  148. // auth.eraseCredentials(false);
  149. // }
  150. }
  151. // @Override
  152. // protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  153. // auth.eraseCredentials(false);
  154. // }
  155. // @Override
  156. // protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  157. // auth.inMemoryAuthentication()
  158. // .withUser("user")
  159. // .password("{noop}user")
  160. // .roles("USER")
  161. // .and()
  162. // .withUser("admin")
  163. // .password("{noop}admin")
  164. // .roles("ADMIN");
  165. //// auth.userDetailsService(myUserDetailsService);
  166. //
  167. // }
  168. // @Bean
  169. // @Override
  170. // public UserDetailsService userDetailsService(String usern) {
  171. //// UserDetails user =
  172. //// User.withDefaultPasswordEncoder()
  173. //// .username("user")
  174. //// .password("password")
  175. //// .roles("USER")
  176. //// .build();
  177. ////
  178. //// return new InMemoryUserDetailsManager(user);
  179. // myUserDetailsService.loadUserByUsername(username)
  180. //
  181. // }
  182. // @Override
  183. // protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  184. // auth.userDetailsService(myUserDetailsService);
  185. // }
  186. // auth.userDetailsService(myUserDetailsService);
  187. // }
  188. }