当前位置:   article > 正文

Jersey中的基本权限认证及访问程序_priority(priorities.authentication)

priority(priorities.authentication)

版本:jersey v2.10


  1. import java.io.IOException;
  2. import java.nio.charset.Charset;
  3. import java.security.Principal;
  4. import javax.annotation.Priority;
  5. import javax.servlet.ServletConfig;
  6. import javax.servlet.ServletContext;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.ws.rs.Priorities;
  9. import javax.ws.rs.container.ContainerRequestContext;
  10. import javax.ws.rs.container.ContainerRequestFilter;
  11. import javax.ws.rs.core.Context;
  12. import javax.ws.rs.core.HttpHeaders;
  13. import javax.ws.rs.core.Response;
  14. import javax.ws.rs.core.SecurityContext;
  15. import javax.ws.rs.ext.Provider;
  16. import org.apache.commons.logging.Log;
  17. import org.apache.commons.logging.LogFactory;
  18. import org.glassfish.jersey.internal.util.Base64;
  19. /**
  20. *
  21. * @author zhangdapeng
  22. * @version 1.0,2014年7月7日
  23. * @since 1.0
  24. */
  25. @Provider
  26. @Priority(Priorities.AUTHENTICATION)
  27. public class AuthRequestFilter implements ContainerRequestFilter {
  28. private Log logger = LogFactory.getLog(getClass());
  29. @Context
  30. HttpServletRequest webRequest;
  31. @Context
  32. ServletConfig servletConfig;
  33. @Context
  34. ServletContext servletContext;
  35. @Override
  36. public void filter(ContainerRequestContext requestContext) throws IOException {
  37. final Charset CHARACTER_SET = Charset.forName("utf-8");
  38. String authHeader = requestContext.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
  39. if (authHeader != null && authHeader.startsWith("Basic")) {
  40. String decoded = new String(Base64.decode(authHeader.substring(6).getBytes()), CHARACTER_SET);
  41. // String decoded = Base64.decodeAsString(authHeader.substring(6));
  42. final String[] split = decoded.split(":");
  43. final String username = split[0];
  44. final String pwd = split[1];
  45. if (pwd.equals(pwd)) {//这里做了最大简化
  46. requestContext.setSecurityContext(new SecurityContext() {
  47. @Override
  48. public Principal getUserPrincipal() {
  49. return new Principal() {
  50. @Override
  51. public String getName() {
  52. return username;
  53. }
  54. };
  55. }
  56. @Override
  57. public boolean isUserInRole(String role) {
  58. return true;
  59. }
  60. @Override
  61. public boolean isSecure() {
  62. return false;
  63. }
  64. @Override
  65. public String getAuthenticationScheme() {
  66. return "BASIC";
  67. }
  68. });
  69. return;
  70. }
  71. }
  72. requestContext.abortWith(Response.status(401).header(HttpHeaders.WWW_AUTHENTICATE, "Basic").build());
  73. //TODO : HERE YOU SHOULD ADD PARAMETER TO REQUEST, TO REMEMBER USER ON YOUR REST SERVICE...
  74. // logger.info(requestContext.getSecurityContext().getUserPrincipal().getName());
  75. }
  76. }

客户端访问:

  1. @Test
  2. public void testBasicPreemptive() {
  3. Client client = ClientBuilder.newClient();
  4. WebTarget target = client
  5. .target("http://localhost:8080/boa/rest/customer/retrieve/1");
  6. Response response = target.register(HttpAuthenticationFeature.basicBuilder().credentials("homer", "country").build())
  7. .request().get();
  8. assertEquals(200, response.getStatus());
  9. Customer c= response.readEntity(Customer.class);
  10. System.out.println("--"+c.getFirstName());
  11. }

参考:http://blog.csdn.net/philosophyatmath/article/details/37879451



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号