ฉันติดตาม tuto ที่ยอดเยี่ยมนี้: https://developer.okta.com/blog/2020/01/06/crud-angular-9-spring-boot-2#spring-boot-as-an-oauth-2-0-resource-server
ฉันต้องการเพิ่ม Roles ให้กับส่วนสปริง ฉันไม่ต้องการใช้การอ้างสิทธิ์เหมือนใน tuto นี้ https://developer.okta.com/blog/2018/09/26/build-a-spring-boot-webapp แต่ฉันต้องการเพิ่มบทบาทศุลกากรของฉัน (จากฐานข้อมูลของฉันเอง)
ฉันพยายามแทนที่ userDetailsService() จาก WebSecurityConfigurerAdapter เพื่อใช้ UserDetailService ที่กำหนดเองของฉัน แต่ดูเหมือนว่าวิธีนี้จะไม่ถูกเรียก ... (ดู println) ควรจะเป็นเช่นนั้นหรือไม่?
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UserRepository userRepository;
@Override
protected void configure(HttpSecurity http) throws Exception {
//@formatter:off
http
.authorizeRequests()
.antMatchers("/home/**").permitAll()
.antMatchers("/admin/**").hasAuthority("ADMIN")
.anyRequest().authenticated()
.and()
.oauth2Login()
.and()
.oauth2ResourceServer().jwt();
//@formatter:on
}
@Override
protected UserDetailsService userDetailsService() {
return username -> {
System.out.println("yo");
Optional<co.simplon.blog.model.User> user = userRepository.findByName(username);
return User
.withUsername(username)
.password(user.get().getPassword())
.authorities(Arrays.asList(user.get().getRole()))
.accountExpired(false)
.accountLocked(false)
.credentialsExpired(false)
.disabled(false)
.build();
};
}
}