我在設置我的安全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>
謝謝你,我做了一個愚蠢的副本。我改變了它,但它仍然不起作用。 –