做视频网站带宽不够怎么办,设计模板app,翻墙在线代理,mysql asp网站idea中使用osgi您首先需要获得对OSGI HTTP Service的引用。 您可以通过声明性服务来做到这一点。 这篇文章将集中在获得对HTTP服务的引用之后的步骤。 注意#xff1a;此职位的完整课程位于此处 通过OSGI HTTP Service注册Servlet时#xff0c;它为您提供了提供HTTPContext实… idea中使用osgi 您首先需要获得对OSGI HTTP Service的引用。 您可以通过声明性服务来做到这一点。 这篇文章将集中在获得对HTTP服务的引用之后的步骤。 注意此职位的完整课程位于此处 通过OSGI HTTP Service注册Servlet时它为您提供了提供HTTPContext实现的选项。 httpService.registerServlet(alias, new MyServlet(), initParams, null); 当我们实现HTTPContext接口时我们可以实现三种方法。 在为ermmm请求之前将调用这三3个handleSecurity中的一部分……检查安全性。 public class BasicAuthSecuredContext implements HttpContext{Overridepublic boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {return false;}Overridepublic URL getResource(String s) {return null; }Overridepublic String getMimeType(String s) {return null;}
} 因此在实现此功能时我从OSGI HTTPContext文档和HTTP Authentication spec中借用了很多内容。 如果您有兴趣学习很多东西深入研究细节等内容则必须阅读它们。或者您可以阅读本文的其余部分。 首先除非使用https否则基本认证非常重要。 如果不存在我们会让用户知道它是禁区。 让我们继续做。 if (!request.getScheme().equals(https)) {response.sendError(HttpServletResponse.SC_FORBIDDEN);return false;
} 接下来让我们检查Authorization标头。 如果那不在那里我们会让他们知道他们需要那种东西才能在那里。 或者我们只是说他们是未经授权的。 现在开始吧。 if (request.getHeader(Authorization) null) {response.sendError(HttpServletResponse.SC_UNAUTHORIZED);return false;
} 好的两项测试通过了。 现在我们做一些实际的工作。 让我们提取标头以对其进行解码然后执行“不太正确”的身份验证。 protected boolean authenticated(HttpServletRequest request) {String authzHeader request.getHeader(Authorization);String usernameAndPassword new String(Base64.decodeBase64(authzHeader.substring(6).getBytes()));int userNameIndex usernameAndPassword.indexOf(:);String username usernameAndPassword.substring(0, userNameIndex);String password usernameAndPassword.substring(userNameIndex 1);// Now, do the authentication against in the way you want, ex: ldap, db stored uname/pw// Here I will do lame hard coded credential check. HIGHLY NOT RECOMMENDED! return ((username.equals(username) password.equals(password));} 让我们将此方法集成到handleSecurity方法中。 请注意当安全性失败时如何将有意义的错误消息设置为响应第14行。 这样可以防止用户猜测并且他们知道出了什么问题。 Ermm至少如果他们知道HTTP错误代码他们将确切知道出了什么问题。 Overridepublic boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {if (!request.getScheme().equals(https)) {response.sendError(HttpServletResponse.SC_FORBIDDEN);return false;}if (request.getHeader(Authorization) null) {response.sendError(HttpServletResponse.SC_UNAUTHORIZED);return false;}if (authenticated(request)) {return true;} else {response.sendError(HttpServletResponse.SC_UNAUTHORIZED);return false;}} 而已。 现在在注册servlet时传递该对象 httpService.registerServlet(alias, new MyServlet(), initParams, new BasicAuthSecuredContext()); …并看到OSGI Servlet中基本身份验证的强大功能 参考 在我们的JCG合作伙伴 Mackie Mathew的dev_religion博客上 在OSGI环境中为Servlet实现Servlet的基本身份验证 。 翻译自: https://www.javacodegeeks.com/2012/06/servlet-basic-auth-in-osgi-environment.htmlidea中使用osgi