簡體   English   中英

使用AngularJS和Spring Security進行身份驗證

[英]Authentication using AngularJS and Spring Security

我想使用AngularJSSpring-BootSpring-security創建一個登錄頁面,我遵循了本教程 ,一切正常。

但這是登錄的靜態方式,它是通過一個application.yml文件完成的,該文件包含如下用戶憑證:

security:
  user:
    name: taha 
    password: password

如何通過檢查Mysql數據庫中的用戶憑據動態創建登錄頁面? 我應該做些什么改變?

第一步,您需要具有域模型User來存儲用戶,然后創建Repositoy和Service層,以從數據庫中按用戶名查找用戶,然后像這樣創建UserDetailService

@Component("userDetailsService")
public class CustomUserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {

    @Autowired
    private IUserService userService;

    private final Logger log = LoggerFactory.getLogger(CustomUserDetailsService.class);

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User userEntity = userService.findByUsername(username);
        if (userEntity != null) {
            log.debug("------------==" + userEntity.getUsername());
            log.debug("------------==" + userEntity.getPassword());
        } else if (userEntity == null)
            throw new UsernameNotFoundException("user not found");

        return userEntity;
    }

}

以及像這樣在“安全性配置”中設置的UserDetailService

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


    @Inject
    @Qualifier("userDetailsService")
    private UserDetailsService userDetailsService;

    @Inject
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .userDetailsService(userDetailsService);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM