我在设置安全 REST 服务时遇到一些问题。 我想创建一个简单的登录/注销服务并使用它。
我正在关注本教程。我跳过了登录表单的部分,并将用户名和密码硬编码到服务中。 (登录()) 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>
我的 Wildflystandalone.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>
最佳答案
您已经定义了两个与 @Path("/logout") 具有相同路径的资源
。资源由其 PATH 唯一标识。请有不同的路径并尝试
关于java - @RolesAllowed 对我的 RESTful 服务没有影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46194676/