springboot或者tomcat 访问报400

控制台报错
java.lang.IllegalArgumentException: The character [_] is never valid in a domain name.
	at org.apache.tomcat.util.http.parser.HttpParser$DomainParseState.next(HttpParser.java:752)
	at org.apache.tomcat.util.http.parser.HttpParser.readHostDomainName(HttpParser.java:605)
	at org.apache.tomcat.util.http.parser.Test.main(Test.java:17)

bug复现

public class Test {
    public static void main(String[] args) {
        String domain="kk_ms.luoluo.com";
        StringReader reader=new StringReader(domain);
        try {
            HttpParser.readHostDomainName(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

分析得原因为域名中包含下划线,在tomcat中HttpParser会对域名进行效验,有下划线会报错

解决方案

在项目中插入包 org.apache.tomcat.util.http.parser,复制HttpParser类并修改

public class HttpParser {
...
private static enum DomainParseState {

 NEW(true, false, false, false, "http.invalidCharacterDomain.atStart"),
 ALPHA(true, true, true, true, "http.invalidCharacterDomain.afterLetter"),
 NUMERIC(true, true, true, true, "http.invalidCharacterDomain.afterNumber"),
 PERIOD(true, false, false, true, "http.invalidCharacterDomain.afterPeriod"),
 HYPHEN(true, true, false, false, "http.invalidCharacterDomain.afterHyphen"),
 //在此添加下环线匹配
 UNDERSCORE(true, false, false, false, "http.invalidCharacterDomain.afterUnderScore"),
 
 COLON(false, false, false, false, "http.invalidCharacterDomain.afterColon"),
 END(false, false, false, false, "http.invalidCharacterDomain.atEnd");
 
  private final boolean mayContinue;
        private final boolean allowsHyphen;
        private final boolean allowsPeriod;
        private final boolean allowsEnd;
        private final String errorMsg;

        private DomainParseState(boolean mayContinue, boolean allowsHyphen, boolean allowsPeriod, boolean allowsEnd, String errorMsg) {
            this.mayContinue = mayContinue;
            this.allowsHyphen = allowsHyphen;
            this.allowsPeriod = allowsPeriod;
            this.allowsEnd = allowsEnd;
            this.errorMsg = errorMsg;
        }

        public boolean mayContinue() {
            return this.mayContinue;
        }

        public HttpParser.DomainParseState next(int c) {
            if (c == -1) {
                if (this.allowsEnd) {
                    return END;
                } else {
                    throw new IllegalArgumentException(HttpParser.sm.getString("http.invalidSegmentEndState", new Object[]{this.name()}));
                }
            } else if (HttpParser.isAlpha(c)) {
                return ALPHA;
            } else if (HttpParser.isNumeric(c)) {
                return NUMERIC;
            } else if (c == 46) {
                if (this.allowsPeriod) {
                    return PERIOD;
                } else {
                    throw new IllegalArgumentException(HttpParser.sm.getString(this.errorMsg, new Object[]{Character.toString((char)c)}));
                }
            } else if (c == 58) {
                if (this.allowsEnd) {
                    return COLON;
                } else {
                    throw new IllegalArgumentException(HttpParser.sm.getString(this.errorMsg, new Object[]{Character.toString((char)c)}));
                }
            } else if (c == 45) {
                if (this.allowsHyphen) {
                    return HYPHEN;
                } else {
                    throw new IllegalArgumentException(HttpParser.sm.getString(this.errorMsg, new Object[]{Character.toString((char)c)}));
                }
            }
            //匹配表达式
            else if (c == 95){
                return UNDERSCORE;
            }
            else{
                throw new IllegalArgumentException(HttpParser.sm.getString("http.illegalCharacterDomain", new Object[]{Character.toString((char)c)}));
            }
        }
    }
}
...
到此就结束了,还是很简单的。
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 内置的 Tomcat 服务器默认只允许通过域名访问,这是为了增强应用的安全性和防止潜在的恶意访问。这种限制是通过配置 Tomcat 的连接器实现的。 在 Spring Boot 中,可以通过配置文件或代码来实现只允许域名访问的限制。 首先,可以在 application.properties 或 application.yml(根据你使用的文件格式)中添加以下配置: ``` server.address=域名或IP地址 ``` 在这个配置中,你可以指定允许访问的域名或IP地址(一般为本机域名或IP地址),Tomcat 只允许通过这个域名或IP地址进行访问。 如果你希望更精确地控制访问权限,可以在代码中进行配置。例如,你可以创建一个类(通常继承自 Spring Boot 的 WebSecurityConfigurerAdapter),然后重写 configure 方法,添加以下代码: ```java @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/**") .hasIpAddress("指定的IP地址或IP地址范围") .and() .csrf() .disable(); } } ``` 在这个配置中,你可以使用 hasIpAddress 方法来指定允许访问的IP地址或IP地址范围。所有其他的请求将被拒绝访问。 通过以上配置,就可以让 Spring Boot 内置的 Tomcat 只允许通过指定的域名或IP地址访问,增加应用的安全性。当然,具体的配置方式还取决于你的应用需求和实际情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值