Www-authenticate Logon Verification

Source: Internet
Author: User
Tags administrator password

public class Basicloginfilter implements Filter {

@Override
public void Dofilter (ServletRequest arg0, Servletresponse arg1,
Filterchain Filterchain) throws IOException, Servletexception {
HttpServletRequest request = (httpservletrequest) arg0;
HttpServletResponse response = (httpservletresponse) arg1;


HttpSession session = Request.getsession ();
String eadloginuserid= (String) session.getattribute ("Acl_login_userid");
if (eadloginuserid!=null) {
Filterchain.dofilter (request, response);
}
if (Eadloginuserid = = null) {
try {
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter ut = Response.getwriter ();
String authorization = Request.getheader ("Authorization");
if (authorization = NULL | | authorization.equals ("")) {
Response.setstatus (401);
Response.setheader ("Www-authenticate", "Basic realm=\", please enter the administrator password \ "");
Response.getwriter (). Print ("Sorry you do not have permission.") ");
Response.getwriter (). Close ();
Return
}
String userandpass = new String (new Base64decoder (). Decodebuffer (Authorization.split ("") [1]));
if (Userandpass.split (":"). Length < 2) {
Response.setstatus (401);
Response.setheader ("Www-authenticate", "Basic realm=\", please enter the administrator password \ "");
Response.getwriter (). Print ("Sorry you do not have permission.") ");
Response.getwriter (). Close ();
Return
}
String users=springproperty.get ("/system.properties", "users");
String[] Permitusersarray=users.split (",");
if (Arrayutils.contains (Permitusersarray, Userandpass)) {
String loguserid=userandpass.split (":") [0];
Session.setattribute ("Acl_login_userid", Loguserid);
Filterchain.dofilter (request, response);
} else {
Response.setstatus (401);
Response.setheader ("Www-authenticate", "Basic realm=\", please enter the administrator password \ "");
Response.getwriter (). Print ("Sorry you do not have permission.") ");
Response.getwriter (). Close ();
Return
}
catch (Exception ex) {
Response.setstatus (401);
Response.setheader ("Www-authenticate", "Basic realm=\", please enter the administrator password \ "");
Response.getwriter (). Print ("Sorry you do not have permission.") ");
Response.getwriter (). Close ();
Return
}
}
}

}



Web.xml configuration:

<filter>
<filter-name>authFilter</filter-name>
<filter-class>com ..... Basicloginfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>authFilter</filter-name>
<url-pattern>/webapp/index.do</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.