Last active
November 15, 2021 21:53
-
-
Save dotkebi/28049abd7d1f581245a1d2a9eba18b92 to your computer and use it in GitHub Desktop.
SpringSecurity Authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class CustomAuthenticationProvider implements AuthenticationProvider { | |
@Autowired | |
private AuthServiceImpl authService; | |
@Override | |
public Authentication authenticate(Authentication authentication) throws AuthenticationException { | |
String username = authentication.getName(); | |
String password = (String) authentication.getCredentials(); | |
AdminUser user = (AdminUser) authService.loadUserByUsername(username); | |
if (user == null || !user.getUsername().equalsIgnoreCase(username)) { | |
throw new BadCredentialsException("Username not found."); | |
} | |
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); | |
if (!passwordEncoder.matches(password, user.getPassword())) { | |
throw new BadCredentialsException("Wrong password."); | |
} | |
Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); | |
return new UsernamePasswordAuthenticationToken(user, password, authorities); | |
} | |
@Override | |
public boolean supports(Class<?> aClass) { | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableWebSecurity | |
public class WebMvcConfig extends WebSecurityConfigurerAdapter { | |
@Override | |
protected void configure(AuthenticationManagerBuilder auth) throws Exception { | |
auth.inMemoryAuthentication() | |
.withUser("userName") | |
.password("password") | |
.roles("ADMIN") | |
; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableWebSecurity | |
public class WebMvcConfig extends WebSecurityConfigurerAdapter { | |
@Override | |
protected void configure(AuthenticationManagerBuilder auth) throws Exception { | |
auth.jdbcAuthentication().dataSource(dataSource) | |
.usersByUsernameQuery( | |
"select username, password, enabled from admin_users where username=?") | |
.authoritiesByUsernameQuery( | |
"select username, role from user_roles where username=?") | |
; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableWebSecurity | |
public class WebMvcConfig extends WebSecurityConfigurerAdapter { | |
@Override | |
protected void configure(AuthenticationManagerBuilder auth) throws Exception { | |
auth.userDetailsService(adminService) | |
; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment