Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. package com.example.letsdoeat.configurations;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.http.HttpMethod;
  8. import org.springframework.security.authentication.AuthenticationManager;
  9. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  10. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  11. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  12. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  13. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  14. import org.springframework.security.core.userdetails.User;
  15. import org.springframework.security.core.userdetails.UserDetails;
  16. import org.springframework.security.core.userdetails.UserDetailsService;
  17. import org.springframework.security.crypto.password.PasswordEncoder;
  18. import org.springframework.security.provisioning.InMemoryUserDetailsManager;
  19. import org.springframework.security.web.AuthenticationEntryPoint;
  20. import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
  21. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  22.  
  23. import javax.sql.DataSource;
  24.  
  25.  
  26. @ComponentScan
  27. @EnableWebSecurity
  28. public class WebSecurity extends WebSecurityConfigurerAdapter {
  29.  
  30. private UserDetailsService userDetailsService;
  31. private PasswordEncoder passwordEncoder;
  32. private AuthenticationEntryPoint restAuthenticationEntryPoint;
  33. private MyAuthenticationSuccessHandler mySuccessHandler;
  34. @Autowired
  35. private DataSource dataSource;
  36.  
  37. // //TODO
  38. @Bean
  39. public DaoAuthenticationProvider authenticationProvider() {
  40. DaoAuthenticationProvider auth = new DaoAuthenticationProvider();
  41. auth.setUserDetailsService(userDetailsService);
  42. auth.setPasswordEncoder(passwordEncoder);
  43. return auth;
  44.  
  45. }
  46.  
  47. //TODO - co to robi
  48. // @Override
  49. // protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
  50. // auth.inMemoryAuthentication()
  51. // .withUser("user1").password(passwordEncoder.encode("user1Pass")).roles("USER")
  52. // .and()
  53. // .withUser("user2").password(passwordEncoder.encode("user2Pass")).roles("USER")
  54. // .and()
  55. // .withUser("admin").password(passwordEncoder.encode("adminPass")).roles("ADMIN");
  56. // }
  57.  
  58. @Override
  59. public void configure(HttpSecurity http) throws Exception {
  60. http.cors().and().csrf().disable()
  61. .exceptionHandling()
  62. .authenticationEntryPoint(restAuthenticationEntryPoint)
  63. .and()
  64. .authorizeRequests()
  65. .anyRequest().permitAll()
  66. .and()
  67. .formLogin()
  68. .successHandler(mySuccessHandler)
  69. .and()
  70. .httpBasic()
  71. .and()
  72. .logout();
  73.  
  74. // http.sessionManagement().maximumSessions(1).expiredUrl("/login?expired=true");//TODO
  75. }
  76.  
  77. @Override
  78. protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
  79. auth.userDetailsService(userDetailsService)
  80. .passwordEncoder(passwordEncoder)
  81. .and()
  82. .authenticationProvider(authenticationProvider())
  83. .jdbcAuthentication()
  84. .dataSource(dataSource);
  85. }
  86.  
  87. @Bean
  88. @Override
  89. public UserDetailsService userDetailsService() {
  90. return new UserDetailsServiceImpl();
  91. }
  92.  
  93. //TODO
  94. @Bean
  95. public AuthenticationManager customAuthenticationManager() throws Exception {
  96. return authenticationManager();
  97. }
  98.  
  99. @Autowired
  100. public void setUserDetailsService(UserDetailsService userDetailsService){
  101. this.userDetailsService = userDetailsService;
  102. }
  103.  
  104. @Autowired
  105. public void setPasswordEncoder(PasswordEncoder passwordEncoder){
  106. this.passwordEncoder = passwordEncoder;
  107. }
  108.  
  109. @Autowired
  110. public void setRestAuthenticationEntryPoint(AuthenticationEntryPoint entryPoint){
  111. this.restAuthenticationEntryPoint = entryPoint;
  112. }
  113.  
  114. @Autowired
  115. public void setMySuccessHandler(MyAuthenticationSuccessHandler mySuccessHandler){
  116. this.mySuccessHandler = mySuccessHandler;
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement