开发者

Using Basic Authentication (htaccess) to restrict access to a specific URL

I need to restrict access to a particular URL, e.g. http://mydomain.com/this/is/the/url on my webserver using Basic Authentication through Apache. Any other URL should be openly accessible. I have seen that you can add specific rules to files using:

<Files "mypage.html">
  Require valid-user
</Files>

My开发者_开发技巧 problem is that all requests are routed to controllers using mod-rewrite and so I don't think that I can restrict access based on the file. Any ideas would be most helpful!


In .htacess file you should put :

AuthType Basic
AuthName "Need to login"
AuthUserFile .htpasswd file location ;
Require user USER

//AuthName is login prompt message
//AuthUserFile  is physical .htpasswd file location i.e.
C:/xampp/htdocs/basic/.htpasswd
//Require user is for a specific user i.e. the username you want to
authenticate

To generate .htpasswd file you can use : - http://www.htaccesstools.com/htpasswd-generator/


I'm not sure if this would work/help, but you could specify something in your application web.xml.

  <security-constraint>
    <display-name>Public access</display-name>
    <web-resource-collection>
      <web-resource-name>PublicPages</web-resource-name>
      <description>Public</description>
      <url-pattern>/servlet/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
      <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
  </security-constraint>
  <security-constraint>
    <display-name>Secured access</display-name>
    <web-resource-collection>
      <web-resource-name>SecuredPages</web-resource-name>
      <description>Secured pages</description>
      <url-pattern>/services/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <description>General Access</description>
      <role-name>*</role-name>
    </auth-constraint>
    <user-data-constraint>
      <description>SSL not required</description>
      <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
  </security-constraint>
  <login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>SecurePages</realm-name>
  </login-config>
  <security-role>
    <description>General Access</description>
    <role-name>*</role-name>
  </security-role>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜