如何限制访问mvc 控制器器或MVC文件夹

SpringMVC给控制器添加自定义注解控制访问权限 - 开源中国社区
当前访客身份:游客 [
当前位置:
发布于 日 19时,
场景描述:现在需要对部分Controller或者Controller里面的服务方法进行权限拦截。如果存在我们自定义的注解,通过自定义注解提取所需的权限值,然后对比session中的权限判断当前用户是否具有对该控制器或控制器方法的访问权限。如果没有相关权限则终止控制器方法执行直接返回。有两种方式对这种情况进行处理。方式一:使用SpringAOP中的环绕Around方式二:使用Spring&web拦截器
代码片段(4)
1.&[代码]定义注解&&&&
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
@Documented
//最高优先级
@Order(Ordered.HIGHEST_PRECEDENCE)
public @interface RoleControl {
* 角色类型,以便决定是否具有相关权限
String value() default "user";
2.&[代码]在Controller中使用&&&&
@RoleControl("ADMIN")
@Controller
public class LoginController {
@Autowired
private UserService uS
@Autowired
private GlobalConfigService gcS
@RoleControl("")
@RequestMapping("/login")
public String login(HttpServletRequest request,HttpServletResponse resp, @ModelAttribute("user") UserDto uDto) {
3.&[代码]方式一:使用SpringAOP中的环绕Around&&&&
@Component
public class RoleControlAspect {
/**类上注解情形 */
// @Pointcut("@within(net.xby1993.springmvc.annotation.RoleControl)")
@Pointcut("execution(* net.xby1993.springmvc.controller..*.*(..)) && @within(net.xby1993.springmvc.annotation.RoleControl)")
public void aspect(){
/**方法上注解情形 */
@Pointcut("execution(* net.xby1993.springmvc.controller..*.*(..)) && @annotation(net.xby1993.springmvc.annotation.RoleControl)")
public void aspect2(){
/**aop实际拦截两种情形*/
@Around("aspect() || aspect2()")
public Object doBefore(ProceedingJoinPoint point) {
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession session=request.getSession();
Object target = point.getTarget();
String method = point.getSignature().getName();
Class&?& classz = target.getClass();
Method m = ((MethodSignature) point.getSignature()).getMethod();
if (classz!=null && m != null ) {
boolean isClzAnnotation= classz.isAnnotationPresent(RoleControl.class);
boolean isMethondAnnotation=m.isAnnotationPresent(RoleControl.class);
RoleControl rc=
//如果方法和类声明中同时存在这个注解,那么方法中的会覆盖类中的设定。
if(isMethondAnnotation){
rc=m.getAnnotation(RoleControl.class);
}else if(isClzAnnotation){
rc=classz.getAnnotation(RoleControl.class);
String value=rc.value();
Object obj=session.getAttribute(GeneUtil.SESSION_USERTYPE_KEY);
String curUserType=obj==null?"":obj.toString();
//进行角色访问的权限控制,只有当前用户是需要的角色才予以访问。
boolean isEquals=StringUtils.checkEquals(value, curUserType);
if(isEquals){
return point.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
4.&[代码]方式二:使用拦截器,推荐&&&&
import java.lang.reflect.M
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpS
import org.slf4j.L
import org.slf4j.LoggerF
import org.springframework.web.method.HandlerM
import org.springframework.web.servlet.handler.HandlerInterceptorA
import net.xby1993.springmvc.annotation.RoleC
import net.xby1993.springmvc.util.GeneU
import net.xby1993.springmvc.util.PathU
import net.xby1993.springmvc.util.StringU
public class GlobalInterceptor extends HandlerInterceptorAdapter{
private static Logger log=LoggerFactory.getLogger(LoginInterceptor.class);
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HttpSession s=request.getSession();
s.setAttribute("host", PathUtil.getHost());
s.setAttribute("siteName", GeneUtil.SITE_NAME);
//角色权限控制访问
return roleControl(request,response,handler);
/**角色权限控制访问*/
private boolean roleControl(HttpServletRequest request,HttpServletResponse response, Object handler){
HttpSession session=request.getSession();
System.out.println(handler.getClass().getName());
if(handler instanceof HandlerMethod){
HandlerMethod hm=(HandlerMethod)
Object target=hm.getBean();
Class&?& clazz=hm.getBeanType();
Method m=hm.getMethod();
if (clazz!=null && m != null ) {
boolean isClzAnnotation= clazz.isAnnotationPresent(RoleControl.class);
boolean isMethondAnnotation=m.isAnnotationPresent(RoleControl.class);
RoleControl rc=
//如果方法和类声明中同时存在这个注解,那么方法中的会覆盖类中的设定。
if(isMethondAnnotation){
rc=m.getAnnotation(RoleControl.class);
}else if(isClzAnnotation){
rc=clazz.getAnnotation(RoleControl.class);
String value=rc.value();
Object obj=session.getAttribute(GeneUtil.SESSION_USERTYPE_KEY);
String curUserType=obj==null?"":obj.toString();
//进行角色访问的权限控制,只有当前用户是需要的角色才予以访问。
boolean isEquals=StringUtils.checkEquals(value, curUserType);
if(!isEquals){
//401未授权访问
response.setStatus(401);
}catch(Exception e){
开源中国-程序员在线工具:
相关的代码(350)
118回/54095阅
317回/51297阅
35回/34193阅
6回/34204阅
35回/32527阅
38回/27308阅
9回/27154阅
39回/24208阅
9回/20846阅
40回/18826阅
开源从代码分享开始
小小懒羊羊的其它代码Posts - 65,
Articles - 0,
Comments - 0
20:38 by 马尔代夫_珍, ... 阅读,
1、发现拷贝的项目,在controller文件夹右击鼠标不能没有&添加控制器&这一项,在控制器里面右键控制器名称也没有&添加视图&,这样就控制器和视图都要手写,开发效率低&。
2、原因是mvc包的问题,解决方法为:先卸载mvc包,再安装一次,再重启vs。
具体步骤:
1)在自己的web项目的&引用&上操作,如下图的&引用&,右击,点击&管理NuGet程序包&。
2)在弹出的如下界面,找到该项,点击&卸载&。
3)卸载完之后就安装这个。
4)一般的情况下,以上的步骤就可以完成了,重启一下项目后,问题就可以解决了。
5)自己的问题:
&&&&&&在第二步的时候,点击&卸载&之后,弹出以下框图,点击&否&。
之后还会出现问题,如下图所示
解决办法:在&更新&里面,查找&Microsoft.AspNet.WebPages&,点击&更新&。(从3.0.0更新到3.2.3版本)
这样,之后就可以接着做第3步了,就可以安装成功。重启动项目,问题解决了!君,已阅读到文档的结尾了呢~~
广告剩余8秒
文档加载中
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
w3school_c# & asp.net教程_
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer--144.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口

我要回帖

更多关于 springmvc前端控制器 的文章

 

随机推荐