java - Servlet - isUserInRole()

标签 java security jakarta-ee servlet-3.0

规范:

小服务程序:3.0
Java: 7
Tomcat :7.0.54

简介:

可以使用 HttpServletRequest.isUserInRole() 方法以编程方式检查用户是否具有特定角色

例如:

public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws IOException, ServletException{

    String username = null;
    String password = null;

    //get username and password manually from Authorization header
    //...
    request.login(username, password);

    if (request.isUserInRole("boss")) {
        //do something
    } else {
        //do something else
    }

    request.logout();

}

这工作正常,但此解决方案需要从授权 header 中手动检索用户名和密码,然后使用这些凭据登录。

问题:

是否可以只做类似的事情?没有从 header 中检索数据并手动登录()?

public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws IOException, ServletException{

    if (request.isUserInRole("boss")) {
        //do something
    } else {
        //do something else
    }

}

试图回答自己:

根据我的理解,这段代码需要在 web.xml 中进行适当的配置。此示例使用 web.xml 文件中的此配置,例如:

<web-app ...>
    ...
    <security-constraint>
        <web-resource-collection>
            <url-pattern>/HelloWorld</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>boss</role-name>
            </auth-constraint>
    </security-constraint>

    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>DefaultRealm</realm-name>
    </login-config>
</web-app>

但这意味着不需要以编程方式检查角色,因为我们只需要在 web.xml 中配置来限制访问。

总结:

  • 是否可以在不在 web.xml 中指定限制(auth-constraint)的情况下以编程方式检查角色?
  • 如果不是,这是否意味着使用 isCallerInRole() 仅执行额外角色的检查,因为所需的主要角色已在 web.xml 中指定?

谢谢。

编辑 1:
由于第一个答案建议将 login-config 元素添加到我的 web.xml,我必须说我已经有了它。我将它添加到代码片段中,因为我在发布问题时没有包含它。示例适用于此配置。但是,当我删除 auth-constraint 或整个 security-constraint 时,login-config 的存在是不够的。 我添加了有关容器的信息:Tomcat 7.0.54。

最佳答案

问题1:

是否可以在不在 web.xml 中指定限制(auth-constraint)的情况下以编程方式检查角色?

回答:

是的,这是可能的。无需在 web.xml 中指定限制。无需将 scurity-contraint 放在 web.xml 中。

此外,无需从 header Authorization 中手动检索凭据,然后手动login()

解决方案:

这是一个工作示例:

public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws IOException, ServletException{

    request.authenticate(response);              //solution

    if (request.isUserInRole("boss")) {
        //do something
    } else {
        //do something else
    }
}

网络.xml:

<web-app ...>
    ...
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>DefaultRealm</realm-name>
    </login-config>
</web-app>

那行得通。

如您所见,HttpServletRequest.authenticate() 方法已被使用,并且可以达到目的。 文档说:

Triggers the same authentication process as would be triggered if the request is for a resource that is protected by a security constraint.

这就是我们所需要的。 我希望它对以后的人有所帮助。

关于java - Servlet - isUserInRole(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24302667/

相关文章:

java - 连接四个垂直赢家

security - 反向代理和网关有什么区别?

python-3.x - 如果在每次使用之前都使用高熵种子进行播种,Python 3 random.random 的安全性是否足够好?

php - 从手机到网站的客户端/服务器安全

jakarta-ee - WFLYEJB0043 : A previous execution of timer [timer] is still in progress, 在 [时间] 跳过这个重叠的计划执行

tomcat - 在企业中使用 Tomcat Web 容器

java - 在 Java EE 6 Web 应用程序中登录用户的不同方式

java - 三个按钮,每个按钮都使用不同的 Int 启动相同的 Activity。但是当选择任何按钮时,它会加载所有 3 个 Activity

java - 通过 Selenium 访问时,DOM 未填充到 PhantomJS 中

java - 为什么我的 JVM 的总内存使用量比它的 Xmx 值大 30 多倍?