springboot后端无法连接来自vue的axios调用

ifsvaxew  于 2021-07-23  发布在  Java
关注(0)|答案(2)|浏览(323)

我正在尝试在前端使用vue,在后端使用springboot来配置登录。但我似乎无法让他们互相交谈。我的vue在端口3000上运行,我的tomcat在端口8080上运行
我的安全-

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .loginProcessingUrl("/sign-in")
            .defaultSuccessUrl("/dashboard", true)
            .permitAll()
            .and()
            .logout().deleteCookies("JSESSIONID")
            .and()
            .rememberMe().key("uniqueAndSecret");
            http.cors();
}

我的控制器-

@Controller
@RestController
@RequestMapping(value = "/api/v1/auth",
                produces = "application/json",
                method = {RequestMethod.GET, RequestMethod.PUT})
public class LoginController {

    @RequestMapping(value = "/sign-in")
    public String login() {
        return "sign-in";
    }
}

我的axios post电话来自“signin.vue”-

submit() {
    let formData = new FormData();
    formData.set("email", this.email);
    formData.set("password", this.password);

    // post api url will be - /api/v1/auth/sign-in after backend and frontend are running on the same port

    axios.post('http://localhost:8080/sign-in', formData,
        {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
    .then(function(res) {
      if(res.data.code === 200) {
        this.router.push('/dashboard')
      }
      else {
        this.msg = response.message;
      }
    })
    .catch(function(err) {
      this.msg = 'error';
    })
  }
mgdq6dx1

mgdq6dx11#

根据logincontroller类级别的requestmapping'/api/v1/auth'和'login()'方法级别的requestmapping'/sign-in',您应该将登录请求发送到url'http://localhost:8080/api/v1/auth/sign-in。

zd287kbt

zd287kbt2#

请查看如何处理视图或自定义登录。页码
您也可以在最新版本中找到类似的文档。

相关问题