NutzCN Logo
问答 关于前端 跨域问题 了nutz封装的 方法
发布于 12天前 作者 qq_8990e1fd 78 次浏览 复制 上一个帖子 下一个帖子
标签:

vue 跨域 nutz 加上了 @Filter 注解之后 前端登录 可以正常 登录但是登录之后的接口访问还是显示跨域问题

浏览器 :Access to XMLHttpRequest at 'http://192.168.1.15:8081/Troops/account/getToken' from origin 'http://localhost:8080' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.POST http://192.168.1.15:8081/Troops/account/getToken net::ERR_FAILEDncaught (in promise) Error: Network Error
    at createError (createError.js?2d83:16)
    at XMLHttpRequest.handleError

后端:package cn.webro.module;

import cn.webro.service.T_accountService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.filter.CrossOriginFilter;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@IocBean
@Fail("http:500")
@At("/account")
@Ok("json:{locked:'password|salt'}")
@AdaptBy(type = JsonAdaptor.class)
@Filters({@By(type = CrossOriginFilter.class)})
public class T_accountModule {

    @Inject
    private T_accountService accountService;

    @At
    public Object login(@Param("username") String id, @Param("password") String password, HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String, Object> login = accountService.login(id, password);
        return login;
    }

    @At
    public Object getToken() {

//        String token = request.getHeader("Authorization");
//        System.out.println("token===========" + token);
//        Enumeration<String> attributeNames = request.getAttributeNames();
//        while (attributeNames.hasMoreElements()) {
//            String key = (String) attributeNames.nextElement();
//            System.out.println("key====" + key);
//            String value = request.getHeader(key);
//            System.out.println("value====" + value);
//        }
        return "sdfdasasd";
    }
}
2 回复

登录日志:2020-11-14 15:58:01,485 [http-nio-8081-exec-108] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [OPTIONS] path=/account/getToken : T_accountModule.getToken(T_accountModule.java:43)
2020-11-14 15:58:01,485 [http-nio-8081-exec-108] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [OPTIONS] path=/account/getToken : T_accountModule.getToken(T_accountModule.java:43)
2020-11-14 15:58:01,485 [http-nio-8081-exec-108] DEBUG org.nutz.ioc.impl.NutIoc - Get 't_accountModule'<class cn.webro.module.T_accountModule>
2020-11-14 15:58:01,485 [http-nio-8081-exec-108] DEBUG org.nutz.ioc.impl.NutIoc - Get 't_accountModule'<class cn.webro.module.T_accountModule>
2020-11-14 15:58:01,485 [http-nio-8081-exec-108] DEBUG org.nutz.mvc.filter.CrossOriginFilter - Feedback -- [] [GET, POST, PUT, DELETE, OPTIONS, PATCH] [Origin, Content-Type, Accept, X-Requested-With] [true]
2020-11-14 15:58:01,485 [http-nio-8081-exec-108] DEBUG org.nutz.mvc.filter.CrossOriginFilter - Feedback -- [
] [GET, POST, PUT, DELETE, OPTIONS, PATCH] [Origin, Content-Type, Accept, X-Requested-With] [true]
2020-11-14 15:58:16,476 [http-nio-8081-exec-109] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [OPTIONS] path=/account/login : T_accountModule.login(T_accountModule.java:26)
2020-11-14 15:58:16,476 [http-nio-8081-exec-109] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [OPTIONS] path=/account/login : T_accountModule.login(T_accountModule.java:26)
2020-11-14 15:58:16,476 [http-nio-8081-exec-109] DEBUG org.nutz.ioc.impl.NutIoc - Get 't_accountModule'<class cn.webro.module.T_accountModule>
2020-11-14 15:58:16,476 [http-nio-8081-exec-109] DEBUG org.nutz.ioc.impl.NutIoc - Get 't_accountModule'<class cn.webro.module.T_accountModule>
2020-11-14 15:58:16,476 [http-nio-8081-exec-109] DEBUG org.nutz.mvc.filter.CrossOriginFilter - Feedback -- [] [GET, POST, PUT, DELETE, OPTIONS, PATCH] [Origin, Content-Type, Accept, X-Requested-With] [true]
2020-11-14 15:58:16,476 [http-nio-8081-exec-109] DEBUG org.nutz.mvc.filter.CrossOriginFilter - Feedback -- [
] [GET, POST, PUT, DELETE, OPTIONS, PATCH] [Origin, Content-Type, Accept, X-Requested-With] [true]
2020-11-14 15:58:16,486 [http-nio-8081-exec-110] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [POST] path=/account/login : T_accountModule.login(T_accountModule.java:26)
2020-11-14 15:58:16,486 [http-nio-8081-exec-110] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [POST] path=/account/login : T_accountModule.login(T_accountModule.java:26)
2020-11-14 15:58:16,487 [http-nio-8081-exec-110] DEBUG org.nutz.ioc.impl.NutIoc - Get 't_accountModule'<class cn.webro.module.T_accountModule>
2020-11-14 15:58:16,487 [http-nio-8081-exec-110] DEBUG org.nutz.ioc.impl.NutIoc - Get 't_accountModule'<class cn.webro.module.T_accountModule>
2020-11-14 15:58:16,596 [http-nio-8081-exec-110] DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor - SELECT a.*,p.*,s.* FROM t_account AS a JOIN t_people AS p ON a.peopleid = p.id JOIN t_status AS s ON p.statusid = s.statusid WHERE a.del = '0'AND p.del = '0' AND (a.accounts = ? OR p.phone = ? OR p.phoneTwo = ? ) AND a.password = ?
| 1 | 2 | 3 | 4 |
|-------------|-------------|-------------|----------------------------------|
| 13655484441 | 13655484441 | 13655484441 | D9840773233FA6B19FDE8CAF765402F5 |
For example:> "SELECT a.*,p.*,s.* FROM t_account AS a JOIN t_people AS p ON a.peopleid = p.id JOIN t_status AS s ON p.statusid = s.statusid WHERE a.del = '0'AND p.del = '0' AND (a.accounts = '13655484441' OR p.phone = '13655484441' OR p.phoneTwo = '13655484441' ) AND a.password = 'D9840773233FA6B19FDE8CAF765402F5'"

登录之后调用别的接口 测试 都再 一个module日志

2020-11-14 15:42:43,763 [http-nio-8081-exec-99] DEBUG org.nutz.ioc.impl.NutIoc - Get 'dao'<interface org.nutz.dao.Dao>
2020-11-14 15:42:43,763 [http-nio-8081-exec-99] DEBUG org.nutz.ioc.impl.NutIoc - Get 'dao'<interface org.nutz.dao.Dao>
2020-11-14 15:42:43,763 [http-nio-8081-exec-99] DEBUG org.nutz.mvc.filter.CrossOriginFilter - Feedback -- [*] [GET, POST, PUT, DELETE, OPTIONS, PATCH] [Origin, Content-Type, Accept, X-Requested-With] [true]
2020-11-14 15:42:43,763 [http-nio-8081-exec-99] DEBUG org.nutz.mvc.filter.CrossOriginFilter - Feedback -- [*] [GET, POST, PUT, DELETE, OPTIONS, PATCH] [Origin, Content-Type, Accept, X-Requested-With] [true]
2020-11-14 15:43:03,322 [http-nio-8081-exec-100] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [OPTIONS] path=/account/getToken : T_accountModule.getToken(T_accountModule.java:43)
2020-11-14 15:43:03,322 [http-nio-8081-exec-100] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [OPTIONS] path=/account/getToken : T_accountModule.getToken(T_accountModule.java:43)
2020-11-14 15:43:03,323 [http-nio-8081-exec-100] DEBUG org.nutz.ioc.impl.NutIoc - Get 't_accountModule'<class cn.webro.module.T_accountModule>
2020-11-14 15:43:03,323 [http-nio-8081-exec-100] DEBUG org.nutz.ioc.impl.NutIoc - Get 't_accountModule'<class cn.webro.module.T_accountModule>
2020-11-14 15:43:03,323 [http-nio-8081-exec-100] DEBUG org.nutz.mvc.filter.CrossOriginFilter - Feedback -- [*] [GET, POST, PUT, DELETE, OPTIONS, PATCH] [Origin, Content-Type, Accept, X-Requested-With] [true]
2020-11-14 15:43:03,323 [http-nio-8081-exec-100] DEBUG org.nutz.mvc.filter.CrossOriginFilter - Feedback -- [*] [GET, POST, PUT, DELETE, OPTIONS, PATCH] [Origin, Content-Type, Accept, X-Requested-With] [true]
2020-11-14 15:46:51,630 [http-nio-8081-exec-106] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [OPTIONS] path=/account/getToken : T_accountModule.getToken(T_accountModule.java:43)
2020-11-14 15:46:51,630 [http-nio-8081-exec-106] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [OPTIONS] path=/account/getToken : T_accountModule.getToken(T_accountModule.java:43)
2020-11-14 15:46:51,630 [http-nio-8081-exec-106] DEBUG org.nutz.ioc.impl.NutIoc - Get 't_accountModule'<class cn.webro.module.T_accountModule>
2020-11-14 15:46:51,630 [http-nio-8081-exec-106] DEBUG org.nutz.ioc.impl.NutIoc - Get 't_accountModule'<class cn.webro.module.T_accountModule>
2020-11-14 15:46:51,630 [http-nio-8081-exec-106] DEBUG org.nutz.mvc.filter.CrossOriginFilter - Feedback -- [*] [GET, POST, PUT, DELETE, OPTIONS, PATCH] [Origin, Content-Type, Accept, X-Requested-With] [true]
2020-11-14 15:46:51,630 [http-nio-8081-exec-106] DEBUG org.nutz.mvc.filter.CrossOriginFilter - Feedback -- [*] [GET, POST, PUT, DELETE, OPTIONS, PATCH] [Origin, Content-Type, Accept, X-Requested-With] [true]

https://gitee.com/budwk/budwk-nutzboot/blob/v6.x-zookeeper/wk-nb-web-admin/src/main/java/com/budwk/nb/web/commons/processor/GlobalsSettingProcessor.java

#是否启用跨域请求
budwk.access.allow.origin.enabled=true
#允许跨域的域名,* 为所有
budwk.access.allow.origin.domain=*
budwk.access.allow.origin.maxage=3600
添加回复
请先登陆
回到顶部