璁板綍涓�涓叧浜� GBK 缂栫爜鐨勯棶棰�

鑳屾櫙

鍖哄垎 UTF-8 鍜� GBK

GBK 鏄湪鍥藉鏍囧噯 GB2312 鍩虹涓婃墿瀹瑰悗鍏煎 GB2312 鐨勬爣鍑嗭紝涓撻棬鐢ㄦ潵瑙e喅涓枃缂栫爜鐨勶紝鏄弻瀛楄妭鐨勶紝涓嶈涓嫳鏂囬兘鏄弻瀛楄妭鐨勩��

UTF-8 鏄竴绉嶅浗闄呭寲鐨勭紪鐮佹柟寮忥紝鍖呭惈浜嗕笘鐣屼笂澶ч儴鍒嗙殑璇鏂囧瓧锛堢畝浣撲腑鏂囧瓧銆佺箒浣撲腑鏂囧瓧銆佽嫳鏂囥�佹棩鏂囥�侀煩鏂囩瓑璇█锛夛紝涔熷吋瀹� ASCII 鐮併��

铏界劧 GBK 姣� UTF-8 灏戣妭鐪佷袱涓瓧鑺傦紝浣嗘槸 GBK 鍙寘鍚腑鏂囷紝UTF-8 鍒欏寘鍚叏涓栫晫鎵�鏈夊浗瀹堕渶瑕佺敤鍒扮殑瀛楃锛屾墍浠ョ幇鍦ㄥ緢澶氱郴缁熸垨鑰呮鏋跺鐞嗛兘榛樿浣跨敤 UTF-8 銆�

涓嶈繃涓氬姟寮�鍙戝鎺ョ郴缁熸帴鍙g殑鏃跺�欙紝缁忓父浼氱鍒颁竴浜涜�佺郴缁熶娇鐢� GBK 缂栫爜锛岀壒鍒槸鍥藉唴鏀粯锛岃繖涓椂鍊欓渶瑕佸吋瀹� GBK 鍜� UTF-8 缂栫爜銆�

濡備綍璁╅」鐩吋瀹� UTF-8 鍜� GBK

鎴戜滑浣跨敤 spring boot 2.7 鐗堟湰銆傛垜浠粯璁PI浣跨敤 UTF-8 锛岀壒鍒殑 API 浣跨敤 GBK銆傚湪 spring boot 涓紝褰撴敹鍒拌姹傝В鏋愬墠浼氶�氳繃 CharacterEncodingFilter 杩涜鍐呭缂栫爜鏍煎紡鎸囧畾銆�

    @Override
    protected void doFilterInternal(
            HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        String encoding = getEncoding();
        if (encoding != null) {
      // 璇锋眰杩涙潵璁剧疆缂栫爜锛堣缃簡璇锋眰寮哄埗杞紪鐮佹垨璇锋眰澶存湭璁剧疆缂栫爜鏃讹級
            if (isForceRequestEncoding() || request.getCharacterEncoding() == null) {
                request.setCharacterEncoding(encoding);
            }
      // 璁剧疆浜嗗搷搴斿己鍒惰浆缂栫爜
            if (isForceResponseEncoding()) {
                response.setCharacterEncoding(encoding);
            }
        }
        filterChain.doFilter(request, response);
    }

閫氳繃浠ヤ笅鏂瑰紡鍙互淇敼椤圭洰鍏ㄥ眬鐨勭紪鐮佹柟寮�

server:
  servlet:
    encoding:
      charset: UTF-8
      force-request: false
      force-response: false

鐪嬩笅榛樿鎯呭喌涓嬶紝缂栫爜鏍煎紡 UTF-8 骞朵笖姣忔閮藉己鍒惰浆鎹紝浠�涔堟剰鎬濓紵涔熷氨鏄氨绠椾綘璇锋眰澶� application/json;charset=GBK 杩欐牱涔熶笉浼氭寜鐓уご鐨勭紪鐮佽В鏋愶紝浼氬己鍒剁粰浣犺浆鎴� UTF-8锛屽鏋滄槸 GBK 杩囨潵鐨勫唴瀹癸紝浣犲氨绛夌潃鍚冧贡鐮佸惂锛�


    @Bean
    @ConditionalOnMissingBean
    public CharacterEncodingFilter characterEncodingFilter() {
        CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
        filter.setEncoding(this.properties.getCharset().name());
        filter.setForceRequestEncoding(this.properties.shouldForce(Encoding.Type.REQUEST));
        filter.setForceResponseEncoding(this.properties.shouldForce(Encoding.Type.RESPONSE));
        return filter;
    }

public boolean shouldForce(Type type) {
        Boolean force = (type != Type.REQUEST) ? this.forceResponse : this.forceRequest;
        if (force == null) {
            force = this.force;
        }
        if (force == null) {
            force = (type == Type.REQUEST);
        }
        return force;
    }

鐜板湪鏈変竴涓敮浠樼郴缁燂紝瀹冭姹傜殑鍐呭鏄� GBK 缂栫爜锛屽苟涓旀湁 GET 鍜� POST 涓ょ鏂瑰紡杩囨潵锛屾垜浠郴缁熸帴鍙i粯璁ゆ槸 UTF-8锛屾墍浠ュ彧鏈夐拡瀵圭壒瀹氱殑 GBK 鎺ュ彛杩涜澶勭悊銆傞渶瑕佹敮鎸佷笅闈㈠嚑绉嶈姹傛儏鍐点��

  1. POST 瀵规柟鐨勮姹傚唴瀹� GBK 缂栫爜骞朵笖璇锋眰澶撮噷鎸囧畾浜嗙紪鐮佹柟寮� application/json;charset=GBK銆�
  2. POST 瀵规柟鐨勮姹傚唴瀹� GBK 缂栫爜骞朵笖璇锋眰澶撮噷鏈寚瀹氱紪鐮佹柟寮� application/json銆�
  3. GET 瀵规柟鐨勮姹傚唴瀹� GBK 缂栫爜銆�

绗竴绉嶆儏鍐碉紝鎴戜滑鍙渶瑕佸叧闂己鍒惰浆鎹紝甯︿簡charset=gbk锛屽氨浼氫娇鐢� gbk 缂栫爜杩涜瑙f瀽锛岄粯璁や笉甯﹀垯浣跨敤 utf-8 瑙f瀽銆�

server:
  servlet:
    encoding:
            force: false

绗簩绉嶅拰绗笁绉嶆儏鍐碉紝鎴戜滑闇�瑕佸厛鍏抽棴寮哄埗杞崲锛岀劧鍚庢坊鍔犱竴涓紭鍏堢骇寰堥珮鐨勮繃婊ゅ櫒灏嗘寚瀹氱殑璇锋眰璁剧疆涓� GBK 缂栫爜鏍煎紡锛堜篃灏辨槸鍦ㄨ繘鍏pring 瑙f瀽鍓嶅氨瑕佸鐞嗭級锛屽鏋滀娇鐢� tomcat 瀹瑰櫒鐨勮瘽濡備笅澶勭悊銆�

@Slf4j
@Configuration
public class GBKFilterConfig {
    @Bean
    public FilterRegistrationBean gbkFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new Filter() {
            @Override
            public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {
            }

            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                    throws IOException, ServletException {
                RequestFacade req = (RequestFacade) request;
                Class clazz = req.getClass();
                log.info("GBK Filter...");
                try {
                    Field field = clazz.getDeclaredField("request");
                    field.setAccessible(true);
                    Request r = (Request) field.get(req);
                    org.apache.coyote.Request p = r.getCoyoteRequest();
                    // GET 璇锋眰鍙傛暟寮轰娇鐢� GBK 缂栫爜銆�
                    p.getParameters().setQueryStringCharset(Charset.forName("GBK"));
                    // POST 璇锋眰甯﹀ご鏈寚瀹氱紪鐮侊紝寮哄埗浣跨敤 GBK
                    p.getParameters().setCharset(Charset.forName("GBK"));
                    p.setCharset(Charset.forName("GBK"));
                    chain.doFilter(request, response);
                } catch (Exception e) {
                   log.error("error", e)
                }
            }

            @Override
            public void destroy() {
            }
        });
        registration.addUrlPatterns("/api/gbk/**");
        registration.setName("gbkFilter");
        registration.setOrder(Integer.MIN_VALUE);
        return registration;
    }
}

你可能感兴趣的:(璁板綍涓�涓叧浜� GBK 缂栫爜鐨勯棶棰�)