【问题标题】:Spring boot Database authentication using Angular使用 Angular 的 Spring Boot 数据库身份验证
【发布时间】:2018-09-10 15:33:13
【问题描述】:

我使用 Spring boot 作为我的 Rest API,使用 Angular 作为我的前端框架。

我是 Angular 的初学者,正在努力使用 Angular 进行身份验证。我在网上看了一些文章,但都很难掌握。

这是我的身份验证生成器配置方法:-

     @Override
 protected void configure(AuthenticationManagerBuilder auth) throws 
 Exception{
     auth.userDetailsService(userDetailsService)
        .passwordEncoder(passwordEncoder());
 }

这是我的配置

    @Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("**/login"))
    .and()
    .authorizeRequests()
        .antMatchers("/profile").hasAnyRole("USER", "ADMIN")
        .antMatchers("admin/**").hasRole("ADMIN")
        .antMatchers("user/**").hasRole("USER")
        .antMatchers("/","/faq","/donate","/recent","/signup","/register").permitAll()
        .antMatchers("/adduser/**").permitAll()
        .antMatchers("/h2/**").permitAll()
        .anyRequest().authenticated()
    .and()
    .formLogin()
        .loginPage("/login")
        .failureUrl("/login?error=true")
        .successHandler(successHandler())
        .defaultSuccessUrl("/user/profile")
        .permitAll()
    .and()
    .logout()
        .logoutSuccessUrl("/login?logout")
        .permitAll();   
}

我已经成功实现了用户认证和授权 使用百里香,但现在我正试图以角度实现它以用于学习目的。

我应该进行 JWT 身份验证(其中包括很多 Spring 的锅炉代码)?有没有其他方法或替代解决方案?

由于找不到任何其他解决方案,我可以通过哪些可能的方式来实现这一点。请帮忙...

【问题讨论】:

标签: spring angular spring-boot


【解决方案1】:

有两种方法,使用 Oauth 或 JWT,我更喜欢 JWT 用于 Spring Boot 应用程序,尽管 JWT 的配置可能会让人不知所措。

在 Spring Boot 上为您的 Angular 应用程序提供 JWT 最直接的解决方案:

Video tutorial by TechPrimers

Source code of the video tutorial

【讨论】:

    猜你喜欢
    • 2019-04-25
    • 2015-08-09
    • 2019-02-07
    • 2022-01-20
    • 2018-03-16
    • 1970-01-01
    • 2018-02-27
    • 2017-04-12
    • 2018-11-17
    相关资源
    最近更新 更多