@RolesAllowed对我的RESTful服务没有任何影响
问题描述:
我在设置我的安全REST服务时遇到了一些麻烦。 我想创建一个简单的登录/注销服务并解决它。@RolesAllowed对我的RESTful服务没有任何影响
我按照这个教程。我跳过登录表单的部分,并将用户名和密码硬编码到服务中。 (登录()) http://www.blog.btbw.pl/java/wildfly-9-login-form-simple-example/
它一切正常,直到我想使用注释@RolesAllowed。 所以我用这个注解创建了一个新的方法(adminInfo())。但是我得出的结论是,注释没有任何区别。我能够成功地调用它,而无需使用“ADMIN”角色进行登录。
也许你们外面聪明的人之一知道我做错了什么,并能够帮助我。对不起,我的语法不好,我不习惯用英语写这么多。
谢谢。
这些都是我的文件:
我用一个简单的服务看起来像这样:
@Context
private HttpServletRequest request;
@GET
@Path("/hello")
public Response hello() {
return Response.ok().entity("Hello, World!").build();
}
@GET
@Path("/logout")
@RolesAllowed("ADMIN")
public Response adminInfo() {
return Response.ok().entity("hello " + request.getUserPrincipal().getName()).build();
}
@POST
@Path("/login")
public Response login() {
try {
request.login("admin", "admin");
return Response.ok().entity("login successful").build();
} catch (Exception e) {
return Response.status(Status.BAD_REQUEST).entity("login failed").build();
}
}
@GET
@Path("/logout")
@RolesAllowed("ADMIN")
public Response logout() {
try {
request.logout();
return Response.ok().entity("logout successful").build();
} catch (Exception e) {
return Response.status(Status.BAD_REQUEST).entity("logout failed").build();
}
}
我的jboss-web.xml文件看起来是这样的:
<jboss-web>
<context-root>/</context-root>
<security-domain>jaas-realm</security-domain>
</jboss-web>
而且我的web.xml如下所示:
<web-app>
<security-constraint>
<web-resource-collection>
<web-resource-name>Authentication</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>ADMIN</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>jaas-realm</realm-name>
</login-config>
<security-role>
<role-name>ADMIN</role-name>
</security-role>
</web-app>
我Wildfly standalone.xml配置是这样的:
<security-domain name="jaas-realm" cache-type="default">
<authentication>
<login-module name="login-module" code="Database" flag="required">
<module-option name="dsJndiName" value="java:/datasource"/>
<module-option name="principalsQuery" value="select password from users where username=?"/>
<module-option name="rolesQuery" value="select rolename, 'Roles' from roles where username=?"/>
<module-option name="hashAlgorithm" value="SHA-256"/>
<module-option name="hashEncoding" value="base64"/>
<module-option name="unauthenticatedIdentity" value="guest"/>
</login-module>
</authentication>
</security-domain>
答
我终于做到了。我必须将注释@Stateless添加到我的服务中。 由于这是5年前发了一个帖子: JAX-WS webservice and @rolesAllowed
我无法找到谷歌的解决方案,但是#1 5分钟是所有我需要的:)
答
您有作为@Path相同的路径( “/注销”)定义的两个资源。资源由其PATH唯一标识。请有不同的路径,并尝试
谢谢你,我做了一个愚蠢的副本。我改变了它,但它仍然不起作用。 –