Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Service
- public class UserDetailServiceImpl implements UserDetailsService {
- @Autowired
- private ClientEntityRepository clientEntityRepository;
- @Override
- public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
- ClientEntity clientEntity = clientEntityRepository.findByUsername(userName);
- if(clientEntity == null){
- throw new UsernameNotFoundException("UserName "+userName+" not found");
- }
- return new UserDetailImpl(clientEntity);
- }
- }
- @Configuration
- @EnableWebSecurity
- @EnableGlobalMethodSecurity(prePostEnabled = true)
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- @Autowired
- private ClientDetailsService clientDetailsService;
- @Autowired
- private ClientEntityWithUserDetailServiceImpl clientEntityWithUserDetailServiceImpl;
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- auth.userDetailsService(clientEntityWithUserDetailServiceImpl)
- .passwordEncoder(passwordEncoder());
- }
- @Bean
- public PasswordEncoder passwordEncoder() {
- return new BCryptPasswordEncoder(8);
- }
- @Override
- @Order(Ordered.HIGHEST_PRECEDENCE)
- protected void configure(HttpSecurity http) throws Exception {
- http
- .sessionManagement()
- .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
- .and()
- .authorizeRequests()
- .formLogin().defaultSuccessUrl("/hi").permitAll();
- }
- @Override
- @Bean
- public AuthenticationManager authenticationManagerBean() throws Exception {
- return super.authenticationManagerBean();
- }
- @Autowired
- private DataSource dataSource;
- @Bean
- public JdbcTokenStore tokenStore() {
- return new JdbcTokenStore(dataSource);
- }
- @Bean
- @Autowired
- public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){
- TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
- handler.setTokenStore(tokenStore);
- handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
- handler.setClientDetailsService(clientDetailsService);
- return handler;
- }
- @Bean
- @Autowired
- public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
- TokenApprovalStore store = new TokenApprovalStore();
- store.setTokenStore(tokenStore);
- return store;
- }
- }
Add Comment
Please, Sign In to add comment