use of org.springframework.web.method.HandlerMethod in project Corgi by kevinYin.
the class AuthorityInterceptor method preHandle.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String uri = getURI(request);
String ip = IpAddressUtils.getClientIpAddr(request);
logger.info("权限拦截器 start | uri: {}, ip: {}", uri, ip);
if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
HandlerMethod method = (HandlerMethod) handler;
// 对只有 MenuResource 注解的方法做处理
RequestMapping requestMapping = method.getMethodAnnotation(RequestMapping.class);
if (method.getMethodAnnotation(MenuResource.class) != null && requestMapping != null) {
long accountId = AdminContext.getAccountId();
AllowAnonymous allowAnonymous = method.getMethodAnnotation(AllowAnonymous.class);
// 没有登录
if (accountId == 0 && allowAnonymous == null) {
response.sendRedirect(AppConstants.SSO_LOGIN_URL);
return false;
}
if (allowAnonymous == null) {
boolean pass = hasAuthorizable(AdminContext.getAccountId(), uri);
// 处理没有权限时的返回值
if (pass == false) {
// 没有权限
logger.error("没有权限访问 | uri: {}", uri);
if (method.getMethodAnnotation(ResponseBody.class) == null) {
// 这是一个页面
response.setCharacterEncoding("UTF-8");
response.sendRedirect("/admin/exception.xhtml?message=" + URLEncoder.encode("没有权限访问", "UTF-8"));
} else {
response.setHeader("Content-Type", "application/json; charset=UTF-8");
response.getWriter().println(DENIED_MESSAGE);
}
return false;
}
}
}
}
logger.info("权限拦截器 end");
return true;
}
use of org.springframework.web.method.HandlerMethod in project free-framework by a601942905git.
the class Application method detectHandlerMethods.
/**
* 扫描URL,如果数据库中不存在,则保存入数据库
* 这个注解很重要,可以在每次启动的时候检查是否有URL更新,RequestMappingHandlerMapping只能在controller层用。这里我们放在主类中
*/
@PostConstruct
public void detectHandlerMethods() {
Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
Set<RequestMappingInfo> mappings = map.keySet();
for (RequestMappingInfo info : mappings) {
HandlerMethod method = map.get(info);
String methodstr = method.toString();
methodstr = methodstr.split("\\(")[0];
methodstr = methodstr.split(" ")[2];
int i = methodstr.lastIndexOf(".");
methodstr = methodstr.substring(0, i);
String urlparm = info.getPatternsCondition().toString();
String url = urlparm.substring(1, urlparm.length() - 1);
}
}
use of org.springframework.web.method.HandlerMethod in project free-framework by a601942905git.
the class CsrfTokenInterceptor method preHandle.
/**
* request请求处理之前
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
// 此处一定要对类型判断,次handler并非一定是HandlerMethod
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
ValidateToken validateToken = method.getAnnotation(ValidateToken.class);
if (null != validateToken && validateToken.vlidate()) {
String requestToken = request.getParameter(CSRF_TOKEN);
boolean validateTokenFlag = validateToken(requestToken);
log.info("CsrfToken验证结果======>" + validateTokenFlag);
// 验证失败
if (!validateTokenFlag) {
return false;
}
// 验证通过移除csrfToken
WebContextUtils.removeSessionAttribute(CSRF_TOKEN);
}
}
return true;
}
use of org.springframework.web.method.HandlerMethod in project fw-cloud-framework by liuweijw.
the class AuthorizationInterceptor method preHandle.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!permissionConfiguration.isEnabled())
return true;
if (!handler.getClass().isAssignableFrom(HandlerMethod.class))
return true;
final HandlerMethod handlerMethod = (HandlerMethod) handler;
final Method method = handlerMethod.getMethod();
final Class<?> clazz = method.getDeclaringClass();
String requestURI = request.getRequestURI();
String modulePermission = "";
// 为了规范,如果class上面没有设置@PrePermissions则不通过
if (!clazz.isAnnotationPresent(PrePermissions.class)) {
log.error("请求[" + requestURI + "]模块上未设置权限,请设置注解@PrePermissions权限!");
R<Boolean> responseWithR = new R<Boolean>().failure("请求[" + requestURI + "]模块上未设置权限,请设置注解@PrePermissions权限!").data(false);
this.handleWithResponse(response, responseWithR);
return false;
}
PrePermissions clazzPermissions = clazz.getAnnotation(PrePermissions.class);
if (!clazzPermissions.required())
return true;
modulePermission = clazzPermissions.value()[0];
// 为了规范:方法上没设置权限的请求则不通过
if (!method.isAnnotationPresent(PrePermissions.class)) {
log.error("请求[" + requestURI + "]方法上未设置权限,请设置注解@PrePermissions权限!");
R<Boolean> responseWithR = new R<Boolean>().failure("请求[" + requestURI + "]方法上未设置权限,请设置注解@PrePermissions权限!").data(false);
this.handleWithResponse(response, responseWithR);
return false;
}
PrePermissions prePermissions = method.getAnnotation(PrePermissions.class);
String[] permissions = prePermissions.value();
if (null == permissions || permissions.length == 0) {
log.error("请求[" + requestURI + "]方法上未正确设置权限,请设置注解@PrePermissions权限!");
R<Boolean> responseWithR = new R<Boolean>().failure("请求[" + requestURI + "]方法上未正确设置权限,请设置注解@PrePermissions权限!").data(false);
this.handleWithResponse(response, responseWithR);
return false;
}
// 验证是否有功能权限
List<String> roleList = JwtUtil.getRole(request);
if (null == roleList || roleList.size() == 0) {
R<Boolean> responseWithR = new R<Boolean>().failure("请求[" + requestURI + "]权限验证失败!").data(false);
this.handleWithResponse(response, responseWithR);
return false;
}
// 所以角色权限集合
Set<String> menuPermissions = new HashSet<String>();
for (String roleCode : roleList) {
menuPermissions.addAll(this.permissionService.findMenuPermissions(roleCode));
}
if (null == menuPermissions || menuPermissions.size() == 0) {
R<Boolean> responseWithR = new R<Boolean>().failure("请求[" + requestURI + "]权限未配置!").data(false);
this.handleWithResponse(response, responseWithR);
return false;
}
for (String permission : permissions) {
String valiatePermission = modulePermission + permission;
log.info("请求[" + requestURI + "],permission:[" + valiatePermission + "]");
// 验证permission是否有功能权限
if (!menuPermissions.contains(valiatePermission)) {
log.info("请求[" + requestURI + "]权限[" + valiatePermission + "]未配置!");
R<Boolean> responseWithR = new R<Boolean>().failure("请求[" + requestURI + "]权限[" + valiatePermission + "]未配置!").data(false);
this.handleWithResponse(response, responseWithR);
return false;
}
}
return true;
}
use of org.springframework.web.method.HandlerMethod in project entando-core by entando.
the class EntandoOauth2Interceptor method preHandle.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod method = (HandlerMethod) handler;
if (method.hasMethodAnnotation(RequestMapping.class)) {
UserDetails user = this.extractOAuthParameters(request);
RestAccessControl rqm = method.getMethodAnnotation(RestAccessControl.class);
if (null == rqm) {
return true;
}
this.checkAuthorization(user, rqm.permission(), request);
}
}
return true;
}
Aggregations