java隐藏显示功能代码_java – 如何根据用户登录隐藏某些功能?

如果你的意思是隐藏一些资源,这取决于用户是否登录,那么这只是限制访问某些页面的问题(参见下面的参考资料).

如果要根据登录的用户隐藏某些功能,则其中一个解决方案是在JSP中检查用户角色并相应地输出内容.

原始例子:

sample.jsp

Sample Page

Content for admin.

Some content here

Another Content

NB!

为了能够使用EL调用带参数的方法,必须使用最小的Servlet版本3.

从这里引用:https://stackoverflow.com/tags/el/info

Since EL 2.2, which is maintained as part of Servlet 3.0 / JSP 2.2

(Tomcat 7, Glassfish 3, JBoss AS 6, etc), it’s possible to invoke

non-getter methods, if necessary with arguments.

根据用户角色隐藏/限制对某些页面的访问的另一种方法是在web.xml中进行安全性配置,或使用注释(最低Java EE 5),或创建自己的过滤器来检查用户的角色请求.

要创建自己的Filter,请创建一个实现javax.servlet.Filter接口的类,并在doFilter()方法中使用HttpServletRequest方法isUserInRole()检查发出请求的用户的角色.

以下是实现自定义过滤器的简单示例:

RoleCheckFilter.java

package com.example.filter;

import java.io.IOException;

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;

/**

* Servlet Filter implementation class RoleCheckFilter.

* Its purpose is to check logged-in user's role and

* and accordingly allow or prevent access to the web resources.

*/

public class RoleCheckFilter implements Filter {

/**

* @see Filter#init(FilterConfig)

*/

public void init(FilterConfig filterConfig) throws ServletException {}

/**

* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)

*/

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)

throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;

HttpServletResponse response = (HttpServletResponse) res;

if (request.isUserInRole("admin")) {

// user have the appropriate rights, allow the request

chain.doFilter(request, response);

} else {

// user does not have the appropriate rights, do something about it

request.setAttribute("error", "You don't have enough rights to access this resource");

response.sendRedirect(request.getContextPath() + "/login.jsp");

// or you could forward a user request somewhere

}

}

/**

* @see Filter#destroy()

*/

public void destroy() {}

}

在web.xml中添加适当的过滤器配置:

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

...

Role Check Filter

com.example.filter.RoleCheckFilter

Role Check Filter

/admin/*

...

当然,在您的情况下,考虑到您使用基本身份验证的事实,在web.xml(声明性安全性)中使用安全配置或使用编程安全性要容易得多.

从官方Java EE文档中引用:

Java EE security services can be implemented for web applications in

the following ways:

Metadata annotations (or simply, annotations) are used to specify information about security within a class file. When the application is deployed, this information can either be used by or overridden by the application deployment descriptor.

Declarative security expresses an application’s security structure, including security roles, access control, and authentication requirements in a deployment descriptor, which is external to the application.

Any values explicitly specified in the deployment descriptor override any values specified in annotations.

Programmatic security is embedded in an application and is used to make security decisions. Programmatic security is useful when declarative security alone is not sufficient to express the security model of an application.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值