前端框架选型是企业提升开发效率与用户体验的关键因素
916
2022-10-18
springboot中@component注解的使用实例
目录@component注解的使用方式一方式二@component注解有什么作用用一句话概括
@component注解的使用
配置响应头的内容。
方式一
直接在-里配置响应头内容。
/**
* ---用户身份确认。
*/
public class RestInterceptor implements HandlerInterceptor {
private static final Logger log = LoggerFactory.getLogger(RestInterceptor.class);
private static final String tokenHeader = "Authorization";
/**
* 返回值:true表示继续流程(如调用下一个-或处理器);false表示流程中断(如登录检查失败),不会继续调用其他的-或处理器,此时我们需要通过response来产生响应;
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("拦截请求");
//响应头
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type,authorization");
response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
if(request.getMethod().equals("OPTIONS")){
response.setStatus(204);
return true;
}
log.info("调用接口:method=" +request.getParameter("method")+",params="+jsON.toJSONString(request.getParameterMap()));
// HandlerMethod handlhttp://erMethod = (HandlerMethod)handler;
// RequestAuth requestAuth = handlerMethod.getMethodAnnotation(RequestAuth.class);
// if(requestAuth!=null && requestAuth.auth()==false){ //非验证API
// return true;//跳过验证
// }
//验证
// String token = request.getHeader(tokenHeader);
// if(token == null || token.equals("")) {
// log.info("没有找到token");
// throw new BusinessException(ResMsg.CODE_TOKEN_NOTFOUND, "没有找到token");
// } else {
// JWSObject jwsObject = JWSObject.parse(token);
// Payload payload = jwsObject.getPayload();
// JSONObject obj = payload.toJSONObject();
// String roleNames = String.valueOf(obj.get("roleNames"));
// Authentication auth = new Authentication();
// if(obj.containsKey("platformId")) {
// Long platformId = Long.valueOf(String.valueOf(obj.get("platformId")));
// auth.setPlatformId(platformId);
// }
// if(obj.containsKey("userid")) {
// Long userId = Long.valueOf(String.valueOf(obj.get("userid")));
// auth.setUserId(userId);
// }
// if(obj.containsKey("orgId")) {
// Long orgId = Long.valueOf(String.valueOf(obj.get("orgId")));
// auth.setOrgId(orgId);
// }
// if(obj.containsKey("orgid")) {//为兼容老版本的接口
// String orgid = String.valueOf(obj.get("orgid"));
// orgid = orgid.replace(",", "");
// Long orgId = Long.valueOf(orgid);
// auth.setOrgId(orgId);
// }
// auth.setRoleNames(roleNames);
// SecurityContext.setContext(auth);
// JwtUtil.verify(jwsObject);
// }
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
//log.info("处理中");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
if(ex != null) {
log.error("接口处理异常:"+ex);
throw ex;
}
}
}
方式二
使用@component注解,将普通javaBean实例化到spring容器中。
public class RestInterceptor implements HandlerInterceptor {
private static final Logger log = LoggerFactory.getLogger(RestInterceptor.class);
private static final String tokenHeader = "Authorization";
/**
* 返回值:true表示继续流程(如调用下一个-或处理器);false表示流程中断(如登录检查失败),不会继续调用其他的-或处理器,此时我们需要通过response来产生响应;
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("拦截请求");
if(request.getMethod().equals("OPTIONS")){
response.setStatus(204);
return true;
}
log.info("调用接口:method=" +request.getParameter("method")+",params="+JSON.toJSONString(request.getParameterMap()));
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModeVtUPLpINOnlAndView modelAndView) throws Exception {
//log.info("处理中");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
if(ex != null) {
log.error("接口处理异常:"+ex);
throw ex;
}
}
}
定义 SimpleCORSFilter.java 类, 使用@component注解
@Component
public class SimpleCORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException VtUPLpINOn{
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type,authorization");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
两种方式都可以完成响应。
@component注解有什么作用
用一句话概括
被@Component注解标识的类,会被纳入Spring容器中统一管理,好处是什么?一句话概括:你不用自己new了!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~