Guest User

Untitled

a guest
Dec 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. @Service
  2. public class UserDetailServiceImpl implements UserDetailsService {
  3.  
  4. @Autowired
  5. private ClientEntityRepository clientEntityRepository;
  6.  
  7. @Override
  8. public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
  9. ClientEntity clientEntity = clientEntityRepository.findByUsername(userName);
  10. if(clientEntity == null){
  11. throw new UsernameNotFoundException("UserName "+userName+" not found");
  12. }
  13. return new UserDetailImpl(clientEntity);
  14.  
  15. }
  16.  
  17. }
  18.  
  19. @Configuration
  20. @EnableWebSecurity
  21. @EnableGlobalMethodSecurity(prePostEnabled = true)
  22. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  23.  
  24. @Autowired
  25. private ClientDetailsService clientDetailsService;
  26.  
  27. @Autowired
  28. private ClientEntityWithUserDetailServiceImpl clientEntityWithUserDetailServiceImpl;
  29.  
  30. @Override
  31. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  32. auth.userDetailsService(clientEntityWithUserDetailServiceImpl)
  33. .passwordEncoder(passwordEncoder());
  34. }
  35.  
  36.  
  37. @Bean
  38. public PasswordEncoder passwordEncoder() {
  39. return new BCryptPasswordEncoder(8);
  40. }
  41.  
  42.  
  43. @Override
  44. @Order(Ordered.HIGHEST_PRECEDENCE)
  45. protected void configure(HttpSecurity http) throws Exception {
  46. http
  47. .sessionManagement()
  48. .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  49. .and()
  50. .authorizeRequests()
  51. .formLogin().defaultSuccessUrl("/hi").permitAll();
  52. }
  53.  
  54. @Override
  55. @Bean
  56. public AuthenticationManager authenticationManagerBean() throws Exception {
  57. return super.authenticationManagerBean();
  58. }
  59.  
  60. @Autowired
  61. private DataSource dataSource;
  62. @Bean
  63. public JdbcTokenStore tokenStore() {
  64. return new JdbcTokenStore(dataSource);
  65. }
  66.  
  67.  
  68. @Bean
  69. @Autowired
  70. public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){
  71. TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
  72. handler.setTokenStore(tokenStore);
  73. handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
  74. handler.setClientDetailsService(clientDetailsService);
  75. return handler;
  76. }
  77.  
  78. @Bean
  79. @Autowired
  80. public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
  81. TokenApprovalStore store = new TokenApprovalStore();
  82. store.setTokenStore(tokenStore);
  83. return store;
  84. }
  85. }
Add Comment
Please, Sign In to add comment