Java常用代码段记录
首字母大写
public static String captureName(String name) { char[] cs=name.toCharArray(); cs[0]-=32; return String.valueOf(cs); }
filter调用service
package com.tonbusoft.uums.filter; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; import com.tonbusoft.uums.commons.utils.IpUtil; import com.tonbusoft.uums.commons.utils.JWTUtils; import com.tonbusoft.uums.commons.utils.SendUtil; import com.tonbusoft.uums.module.ua.bean.UaDict; import com.tonbusoft.uums.module.ua.service.UaDictService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 请求过滤器 */ public class JWTFilter implements Filter{ public FilterConfig config; public UaDictService uaDictService; public ApplicationContext ctx = null; @Override public void destroy() { this.config = null; } //过滤请求 认证token有效性 @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest hrequest = (HttpServletRequest)request; HttpServletResponseWrapper hresponse = new HttpServletResponseWrapper((HttpServletResponse) response); String path = hrequest.getRequestURI(); if (path.indexOf("/UUMS/webservice/myService") > -1) { //是UUMS供第三方调用的接口 //判断是否需要进行token有效性验证 String action = path.substring(path.lastIndexOf("/")+1,path.length()); //获取数据库配置 boolean validateFlag = false; List<UaDict> list = uaDictService.findByFbh("2"); if (null != list && list.size() > 0) { validateFlag = true; } if (validateFlag) { //验证token失败 Map<String, String> tokenMap = new HashMap<>(); tokenMap.put("token_validate", "error_token_validate"); SendUtil.write(hresponse, tokenMap); } else { //不验证token chain.doFilter(request, response); return; } } else if (path.indexOf("/UUMS") > -1) { chain.doFilter(request, response); return; } } @Override public void init(FilterConfig filterConfig) throws ServletException { config = filterConfig; ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); uaDictService =(UaDictService) ctx.getBean("uaDictService"); } }