繁体   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