微人事-权限管理继续完善

1 当用户直接输入地址的时候会被服务器要求重定向到http://localhost:8081/login 要求用户登录用才可以访问 当后端将http://localhost:8081/login返回给前端,前端会直接访问这个地址,就不经过node.js代理。没有代理就跨域了。
解决1 在login的接口上使用@CrossOrigin注释
解决2

protected void configure(HttpSecurity http) throws Exception

以上方法中加入以下代码

                .csrf().disable()
                // 没有认证时,在这里处理结果,不要重定向
                .exceptionHandling().authenticationEntryPoint(new AuthenticationEntryPoint() {
            @Override
            public void commence(HttpServletRequest req, HttpServletResponse resp, AuthenticationException e) throws IOException, ServletException {
                resp.setContentType("application/json;charset=utf-8");
                PrintWriter out = resp.getWriter();
                RespBean error = RespBean.error("访问失败");
                if (e instanceof InsufficientAuthenticationException){
                    error.setMsg("尚未登录,请登录(请求失败)");
                }
                out.write(new ObjectMapper().writeValueAsString(error));
                out.flush();
                out.close();
            }

2 当用户直接输入地址后,不能还停留在空白页,要返回到home页

在前置导航守卫中
如下判断
如果用户去的是home直接去
否则判断是由存有用户信息,有正常跳转,没有跳转home页

  if (to.path == '/'){
    next();
  }else{
    if (window.sessionStorage.getItem("user")) {
      initMenu(router,store);
      next();
    }else{
      next('/')
    }
  }

3 实现当用户直接输入地址后,跳转home页,当用户登录后直接跳转到用户直接输入的地址位置

如果用户去的是home直接去
否则判断是由存有用户信息,有正常跳转,没有拼接重定向

  if (to.path == '/'){
    next();
  }else{
    if (window.sessionStorage.getItem("user")) {
      initMenu(router,store);
      next();
    }else{
      next('/?redirect='+to.path)
    }
  }

以下是登录页面的登录方法

            submitLogin(){
                    this.$refs.loginForm.validate((valid) => {
                        if (valid) {
                            postKeyValueRequest('/doLogin',this.loginForm).then(resp=>{
                                if (resp) {
                                    window.sessionStorage.setItem("user",JSON.stringify(resp.obj))
                                    // 获取redirect重定向的地址
                                    let path = this.$route.query.redirect;
                                   // path是home页或者没定义直接跳home页 否则跳重定向的地址
                                    this.$router.replace((path == '/'||path == undefined)?'/home':path)
                                }
                            })
                        } else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                }

猜你喜欢

转载自blog.csdn.net/weixin_39232166/article/details/106631526
今日推荐