JBOSS AS7 jax-rs jaas and annotations

做~自己de王妃 提交于 2020-01-16 00:46:35

问题


Hey I hope someone can help tried for last 2 days to figure out what I am doing wrong. I want a programmatic login to a JAX-RS api and to use @RolesAlowed annotations on my endpoints.

I login fine and can see the principal set in the login endpoint also I get a JSESSIONID. But when I call the /info/ping endpoint that is annotated with @RolesAllowed("USER") it throws UnauthenticatedException. If I remove the annotation then the req.getUserPrincipal() is null event though the cookie set.

Any help would be appreciated.

I am using jboss as 7

setup as follows:

Standalone.xml

<security-domain name="api" cache-type="default">
    <authentication>
        <login-module code="Database" flag="required">
            <module-option name="dsJndiName" value="java:jboss/datasources/MysqlDS"/>
            <module-option name="principalsQuery" value="select password from SiteUser where email=?"/>
            <module-option name="rolesQuery" value="select role, 'Roles' from user_roles where email=?"/>
            <module-option name="hashAlgorithm" value="MD5"/>
            <module-option name="hashEncoding" value="base64"/>
            <module-option name="unauthenticatedIdentity" value="GUEST"/>
        </login-module>
    </authentication>
</security-domain>

Web.xml

<context-param>
    <param-name>resteasy.role.based.security</param-name>
    <param-value>true</param-value>
</context-param>
<filter>
    <filter-name>Resteasy</filter-name>
    <filter-class>
        org.jboss.resteasy.plugins.server.servlet.FilterDispatcher
    </filter-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.surecoin.api.rest.RootApplication</param-value>
    </init-param>
    <init-param>
        <param-name>resteasy.scan</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<security-constraint>
    <web-resource-collection>
        <web-resource-name>admin_resources</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
        <http-method>PUT</http-method>
        <http-method>DELETE</http-method>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>
<!-- Login Prompt -->
<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
        <form-login-page>/login/login.xhtml</form-login-page>
        <form-error-page>/error.xhtml</form-error-page>
    </form-login-config>    
</login-config>
<security-role>
    <description>Users</description>
    <role-name>USER</role-name>
</security-role>
<security-role>
    <role-name>guest</role-name>
</security-role>
<security-role>
    <description>Admin</description>
    <role-name>ADMIN</role-name>
</security-role>
<security-role>
    <role-name>GUEST</role-name>
</security-role>

jboss-web.xml

<jboss-web>
    <security-domain>api</security-domain>
</jboss-web>

And the login code :

@POST
@Path("login")
@Produces(MediaType.APPLICATION_JSON)
public Response login(@FormParam("email") String email, @FormParam("password") String password,
        @Context HttpServletRequest req) {
    String username = email; 
    //only login if not already logged in...
    if(req.getUserPrincipal() == null) {
        try {
            req.login(username, password);
            System.out.println(req.getUserPrincipal().toString());
        }
        catch(ServletException e){
            e.printStackTrace();
            return Response.status(Response.Status.UNAUTHORIZED).build();
        }

    } else {
        System.out.println(req.getUserPrincipal().toString());
        req.getServletContext().log("Skip logged because already logged in: "+ username);
    }

    req.getServletContext().log("Authentication Demo: successfully retrieved User Profile from DB for " + username);
    return Response.ok().build();
}


Finally the security check:
@Path("/info/ping")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed("USER")
@GET
public String ping(@Context HttpServletRequest req){
    System.out.println(req.getUserPrincipal());
    System.out.println(req.isUserInRole("USER"));
    return "{\"status\":\"ok\"}";
}

回答1:


Principal in Servlet 3.0 is session scoped. Make sure the second request is mapped/using the same session (use the JSESSIONID).



来源:https://stackoverflow.com/questions/17202398/jboss-as7-jax-rs-jaas-and-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!