无法通过Reactjs在Springboot Security中进行身份验证

时间:2020-11-02 21:28:47

标签: reactjs spring-boot spring-boot-admin spring-boot-starter-security

我目前正在使用Springboot的后端和Reactjs的前端构建应用程序。 springboot的安全功能很好。后端在http:// localhost:8080 /上运行,而Reactjs(前端)在http:// localhost:3000 /上运行。 如何在我的Reactjs登录页面上获取默认的springboot登录页面。

这是我的SecurityConfiguration类

@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
...
  @Override
    protected void configure(HttpSecurity http) throws Exception {
       http.authorizeRequests()
               .antMatchers("/").permitAll()
               .antMatchers("/login/*").permitAll()
               .antMatchers("/createNewUser/*").hasAnyRole("Admin")
               .antMatchers("/deleteUser/*").hasAnyRole("Admin")
               .antMatchers("/test1/**").hasAnyRole("Admin","RegularUser")
               .antMatchers("/test5/**").hasAnyRole("Admin")
               .antMatchers("/test2/**").authenticated()
               .antMatchers("/test4/**").authenticated()
               //               .antMatchers("/test/**").hasAnyRole("ADMIN")
               .and().formLogin()
               .loginProcessingUrl("/login/process")
               .successHandler(customLoginSuccessHandler)
               .and().csrf().disable()
               .logout(logout -> logout
               .permitAll()
               .logoutUrl("/logout")
               .logoutSuccessHandler((request, response, authentication) -> {
                           response.setStatus(HttpServletResponse.SC_OK);
                       }
               ));
    }
}

对于其他不受保护的URL,我可以在我的React应用程序上毫无问题地访问它。 但是当我使用Axios在Reactjs中调用例如http:// localhost:8080 / test2 /时,我得到403错误(禁止访问)。 但是在浏览器上,当我调用相同的URL时,我可以对自己进行身份验证并访问所需的资源。 因此,可以得出结论,这两个应用程序可以完美运行,但是它们之间没有任何联系。

1 个答案:

答案 0 :(得分:0)

通过许多研究,我终于得到了我的问题的答案。 我需要实现JWT(Json Web令牌) 然后删除,因为验证是通过Url

完成的
 .and().formLogin()
               .loginProcessingUrl("/login/process")
               .successHandler(customLoginSuccessHandler)
               .and().csrf().disable()
               .logout(logout -> logout
               .permitAll()
               .logoutUrl("/logout")
               .logoutSuccessHandler((request, response, authentication) -> {
                           response.setStatus(HttpServletResponse.SC_OK);
                       }
               ));