【问题标题】:Authentication using AngularJS and Spring Security使用 AngularJS 和 Spring Security 进行身份验证
【发布时间】:2017-08-14 18:37:16
【问题描述】:

我想使用 AngularJSSpring-BootSpring-security 创建一个登录页面工作正常。

但这是一种静态登录方式,它是通过包含如下用户凭据的 application.yml 文件完成的:

security:
  user:
    name: taha 
    password: password

如何通过检查 Mysql 数据库中的用户凭据来动态创建登录页面?我应该做哪些改变?

【问题讨论】:

  • 根据教程。 “第一部分我们构建了一个简单的应用程序,它使用 HTTP Basic 身份验证来保护后端资源。”
  • 您将创建 UserDetailService 并处理 authenticate ,

标签: mysql angularjs spring-boot spring-security


【解决方案1】:

第一步你需要有域模型 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 {

    }

}

【讨论】:

  • 感谢您的帮助,但我是 spring 环境的新手,您能解释一下将这些文件放在用户模型和用户存储库的同一目录中还是另一个文件夹中的位置?同样在配置方法的教程中,他使用了 httpBasic() 我需要更改它吗?
  • 我想你需要一些例子才能知道,阅读 jhipster.github.io 或其他网站
猜你喜欢
  • 2011-10-17
  • 2014-07-03
  • 2012-11-27
  • 2018-11-07
  • 1970-01-01
  • 2018-08-09
  • 2012-11-10
  • 2017-04-15
相关资源
最近更新 更多