简体   繁体   English

使用AngularJS和Spring Security进行身份验证

[英]Authentication using AngularJS and Spring Security

I want to create a login page using AngularJS , Spring-Boot and Spring-security I follow this tutorial and every thing works fine. 我想使用AngularJSSpring-BootSpring-security创建一个登录页面,我遵循了本教程 ,一切正常。

But It's a static way to do login it's done with an application.yml file containing user credentials like this: 但这是登录的静态方式,它是通过一个application.yml文件完成的,该文件包含如下用户凭证:

security:
  user:
    name: taha 
    password: password

how can I create a login page dynamicaly by checking user credentials in Mysql database ? 如何通过检查Mysql数据库中的用户凭据动态创建登录页面? what changes should I made ? 我应该做些什么改变?

First step you need have domain model User to store users and then create Repositoy and Service layer that find user by username from database and then create UserDetailService like this 第一步,您需要具有域模型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;
    }

}

and the set UserDetailService in Security Configuration like this 以及像这样在“安全性配置”中设置的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