เพิ่มบทบาทในฤดูใบไม้ผลิด้วย userDetailsService ที่กำหนดเองโดยใช้การตรวจสอบสิทธิ์ okta

ฉันติดตาม 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();
        };
    }


}

person Josselin Tobelem    schedule 23.03.2020    source แหล่งที่มา
comment
สวัสดี @Josselin คุณช่วยเข้าใจปัญหานี้ได้ไหม ฉันมีข้อกำหนดเดียวกัน แต่ก็ไม่สามารถหาทางแก้ไขได้   -  person Biky    schedule 15.09.2020
comment
สวัสดี @biky ฉันไม่เข้าใจปัญหานี้ เป็นเรื่องปกติสำหรับฉันที่จะใช้ jwt (สปริง) + firebase (เชิงมุม)   -  person Josselin Tobelem    schedule 16.09.2020


คำตอบ (1)


คุณต้องเพิ่มการใช้งาน UserDetailsService ใน WebConfigurerSEcurityAdapter:

    @Override
public void configure(AuthenticationManagerBuilder builder)
        throws Exception {
    builder.userDetailsService(new MyUserDetailsService());
}

จากนั้นคุณจะเห็นว่าการใช้งานของคุณจะเริ่มถูกเรียก

person Fabio Cabrini    schedule 12.04.2020