Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.security;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.HttpMethod;
- import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
- import org.springframework.security.config.annotation.web.builders.HttpSecurity;
- import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
- import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
- @Configuration
- @EnableWebSecurity
- public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http.csrf().disable().authorizeRequests()
- .antMatchers("/").permitAll()
- .antMatchers(HttpMethod.POST, "/login").permitAll()
- .anyRequest().authenticated()
- .and()
- // We filter the api/login requests
- .addFilterBefore(new JWTLoginFilter("/login", authenticationManager()),
- UsernamePasswordAuthenticationFilter.class)
- // And filter other requests to check the presence of JWT in header
- .addFilterBefore(new JWTAuthenticationFilter(),
- UsernamePasswordAuthenticationFilter.class);
- }
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- // Create a default account
- auth.inMemoryAuthentication()
- .withUser("admin")
- .password("password")
- .roles("ADMIN");
- }
- }
- @Autowired
- private CustomUserDetailService userDetailsService;
- @Override
- public void configure(AuthenticationManagerBuilder auth) throws Exception {
- auth
- .userDetailsService(userDetailsService)
- ;
- }
- @Service
- public class CustomUserDetailService implements UserDetailsService {
- @Override
- public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
- User user = getUserFromDatabase();
- UserItem userItem = new UserItem(user.getUsername(),user.getPassword(),true,true,true,true, new ArrayList<GrantedAuthority>());;
- userItem.setAuthorities(AuthorityUtils.createAuthorityList("ROLE_ADMIN", "ROLE_USER"));
- return userItem;
- }
- }
- @Bean
- public DriverManagerDataSource dataSource() {
- DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
- driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
- driverManagerDataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/mydb");
- driverManagerDataSource.setUsername("postgres");
- driverManagerDataSource.setPassword("root");
- return driverManagerDataSource;
- }
- @Autowired
- DataSource dataSource;
- @Bean(name="passwordEncoder")
- public PasswordEncoder passwordencoder(){
- return new BCryptPasswordEncoder();
- }
- public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
- auth.jdbcAuthentication().dataSource(dataSource)
- .usersByUsernameQuery(
- "select email,password from users where email=?").passwordEncoder(passwordencoder());
- }
Add Comment
Please, Sign In to add comment