Guest User

Untitled

a guest
Jul 17th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. package com.example.security;
  2.  
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.http.HttpMethod;
  5. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  6. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  7. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  8. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  9. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  10.  
  11. @Configuration
  12. @EnableWebSecurity
  13. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  14. @Override
  15. protected void configure(HttpSecurity http) throws Exception {
  16. http.csrf().disable().authorizeRequests()
  17. .antMatchers("/").permitAll()
  18. .antMatchers(HttpMethod.POST, "/login").permitAll()
  19. .anyRequest().authenticated()
  20. .and()
  21. // We filter the api/login requests
  22. .addFilterBefore(new JWTLoginFilter("/login", authenticationManager()),
  23. UsernamePasswordAuthenticationFilter.class)
  24. // And filter other requests to check the presence of JWT in header
  25. .addFilterBefore(new JWTAuthenticationFilter(),
  26. UsernamePasswordAuthenticationFilter.class);
  27. }
  28.  
  29. @Override
  30. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  31. // Create a default account
  32. auth.inMemoryAuthentication()
  33. .withUser("admin")
  34. .password("password")
  35. .roles("ADMIN");
  36. }
  37. }
  38.  
  39. @Autowired
  40. private CustomUserDetailService userDetailsService;
  41.  
  42. @Override
  43. public void configure(AuthenticationManagerBuilder auth) throws Exception {
  44. auth
  45. .userDetailsService(userDetailsService)
  46. ;
  47. }
  48.  
  49. @Service
  50. public class CustomUserDetailService implements UserDetailsService {
  51.  
  52.  
  53. @Override
  54. public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
  55.  
  56. User user = getUserFromDatabase();
  57.  
  58. UserItem userItem = new UserItem(user.getUsername(),user.getPassword(),true,true,true,true, new ArrayList<GrantedAuthority>());;
  59.  
  60. userItem.setAuthorities(AuthorityUtils.createAuthorityList("ROLE_ADMIN", "ROLE_USER"));
  61. return userItem;
  62. }
  63. }
  64.  
  65. @Bean
  66. public DriverManagerDataSource dataSource() {
  67. DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
  68. driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
  69. driverManagerDataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/mydb");
  70. driverManagerDataSource.setUsername("postgres");
  71. driverManagerDataSource.setPassword("root");
  72. return driverManagerDataSource;
  73. }
  74.  
  75. @Autowired
  76. DataSource dataSource;
  77.  
  78. @Bean(name="passwordEncoder")
  79. public PasswordEncoder passwordencoder(){
  80. return new BCryptPasswordEncoder();
  81. }
  82.  
  83. public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
  84. auth.jdbcAuthentication().dataSource(dataSource)
  85. .usersByUsernameQuery(
  86. "select email,password from users where email=?").passwordEncoder(passwordencoder());
  87. }
Add Comment
Please, Sign In to add comment