Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Servlet-isUserInRole()_Java_Security_Jakarta Ee_Servlet 3.0 - Fatal编程技术网

Java Servlet-isUserInRole()

Java Servlet-isUserInRole(),java,security,jakarta-ee,servlet-3.0,Java,Security,Jakarta Ee,Servlet 3.0,规格: Servlet:3.0 爪哇:7 雄猫:7.0.54 简介: 可以使用方法HttpServletRequest.isUserInRole()以编程方式检查用户是否具有特定角色 例如: <web-app ...> ... <security-constraint> <web-resource-collection> <url-pattern>/HelloWorld</url-pat

规格:

Servlet:3.0
爪哇:7
雄猫:7.0.54

简介:

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

例如:

<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>
public void doGet(HttpServletRequest请求,HttpServletResponse响应)
抛出IOException、ServletException{
字符串username=null;
字符串密码=null;
//从授权标头手动获取用户名和密码
//...
请求。登录(用户名、密码);
if(request.isUserInRole(“boss”)){
//做点什么
}否则{
//做点别的
}
请求。注销();
}
这很好,但此解决方案需要从授权头手动检索用户名和密码,然后使用这些凭据登录

问题:

有可能就这样做吗?没有从标头检索数据并手动登录()

public void doGet(HttpServletRequest请求,HttpServletResponse响应)
抛出IOException、ServletException{
if(request.isUserInRole(“boss”)){
//做点什么
}否则{
//做点别的
}
}
试着回答自己:

据我所知,这段代码需要在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约束)
  • 如果不是,这是否意味着使用isCalerInRole()只检查其他角色,因为在web.xml中指定了所需的主要角色
谢谢

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

以下是您问题的答案,如果您使用的是基本身份验证,请添加以下内容:

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>ourRealm</realm-name>
</login-config>

基本的
我们的王国

web.xml中的servlet提供的基本授权机制是基本的,大部分是“硬编码的”

如果您想实现一种更复杂的方式来检查用户角色/授权,您需要保护servlet,那么您有几种可能性:

  • 正确实现JAAS。几乎没有教程;这是给你的
  • 一个可能更强大的替代方法是使用

问题1:

是否可以通过编程检查角色而不在web.xml中指定限制(auth约束)

答复:

是的,这是可能的。不需要在web.xml中指定限制。不需要在web.xml中添加scurity约束

此外,不需要从标头授权中手动检索凭据,然后手动登录()

解决方案:

以下是一个工作示例:

public void doGet(HttpServletRequest请求,HttpServletResponse响应)
抛出IOException、ServletException{
request.authenticate(response);//解决方案
if(request.isUserInRole(“boss”)){
//做点什么
}否则{
//做点别的
}
}
web.xml:


...
基本的
默认领域
这就行了

正如您所看到的,使用了方法HttpServletRequest.authenticate(),并且实现了这个技巧。 文件说:

触发与请求受安全约束保护的资源时触发的身份验证过程相同的身份验证过程

这就是我们所需要的。
我希望它能帮助将来的人。

@win\u wave,我已经有了它,并且我在我的代码片段中添加了它(参见edit1)。然后,可能最好使用Shiro