Verify that the user has logged on and has enabled Automatic Logon (50) and automatic logon 50

Source: Internet
Author: User

Verify that the user has logged on and has enabled Automatic Logon (50) and automatic logon 50

Verify that the user has logged on

PackageCn. hongxin. filter;

ImportJava. io. IOException;

ImportJavax. servlet. Filter;

ImportJavax. servlet. FilterChain;

ImportJavax. servlet. FilterConfig;

ImportJavax. servlet. ServletException;

ImportJavax. servlet. ServletRequest;

ImportJavax. servlet. ServletResponse;

ImportJavax. servlet. http. HttpServletRequest;

ImportJavax. servlet. http. HttpServletResponse;

ImportJavax. servlet. http. HttpSession;

 

Public ClassLoginFilterImplementsFilter {

Public VoidInit (FilterConfig filterConfig)ThrowsServletException {

}

Public VoidDoFilter (ServletRequest request, ServletResponse response,

FilterChain chain)ThrowsIOException, ServletException {

// Convert the request to htt...

HttpServletRequest req = (HttpServletRequest) request;

// Obtain the session

HttpSession ss = req. getSession ();

// Obtain the user from the session

If(Ss. getAttribute ("user") =Null){

System.Err. Println ("You have not logged on ");

Req. getSession (). setAttribute ("msg", "Please log on first ");

// Redirect to logon

HttpServletResponse resp = (HttpServletResponse) response;

Resp. sendRedirect (req. getContextPath () + "/index. jsp"); [W2]

}Else{

// Allow

Chain. doFilter (request, response );

}

}

Public VoidDestroy (){

}

}

Configure to web. xml and overwrite jsps:

<Filter>

<Filter-name> login </filter-name>

<Filter-class> cn. itcast. filter. LoginFilter </filter-class>

</Filter>

<Filter-mapping>

<Filter-name> login </filter-name>

<Url-pattern>/jsps/* </url-pattern>

<Url-pattern>/views/* </url-pattern>

</Filter-mapping>

Automatic Login

 

Automatic Logon is used to help users log on to the webpage multiple times without entering the user name and password.

 

The user saves the user's login information, user, to the Cookie in a local file.

 

Name, value-new Cookie (key, value) When declaring );

 

Path-default value, that is, the Path of the serlvet that currently saves the cookie.

If the Cookie is in the following path: http: // loclhost: 8080/project/abc/AServlet

The Cookie Path is http: // loclhost/project/abc.

Note:

The servlet in the http: // loclhost/project/abc directory can read the cookie value.

 

If:

Save Cookie: http: // loclhost: 8080/project/a/B/AServlet

The default path of the Cookie is;

Http: // loclhost/project/a/B

Step 1: Develop a logon page

<C: choose>

<C: when test = "$ {EmptySessionScope. name} ">

<Form name ="X"Method ="Post"Action ="<C: url value ='/LoginServlet'/>">

Name: <input type ="Text"Name ="Name"/> <Br/>

Auto:

<Input type ="Radio"Name ="Auto"Value ="-1"> Do not log on automatically

<Br/>

<Input type ="Radio"Name ="Auto"Value ="1"> 1 day <br/>

<Input type ="Radio"Name ="Auto"Value ="7"> 1 week <br/>

<Input type ="Submit"/>

</Form>

</C: when>

<C: otherwise>

You have logged on: $ {name} <br/>

<A href ="<C: url value ='/LoginServlet'/>"> Exit </a>

</C: otherwise>

</C: choose>

Step 2: successfully Save the cookie

Public VoidDoPost (HttpServletRequest request, HttpServletResponse response)

ThrowsServletException, IOException {

// Receive user name

String name = request. getParameter ("name ");

String auto = request. getParameter ("auto ");

// Put the user information to the session

Request. getSession (). setAttribute ("name", name );

// Determine whether auto is-1

If(! Auto. equals ("-1 ")){

IntDay = Integer.ParseInt(Auto); // 1 | 7

IntSeconds = 60*60*24 * day;

// Declare cookie

Cookie c =NewCookie ("autoLogin", name );

C. setMaxAge (seconds );

C. setPath (request. getContextPath ());

// Save the cookie

Response. addCookie (c );

}

}

 

Step 3: Enable Automatic Logon when you access any page of the site

Write a scanner to overwrite all URLs =. Read all cookies in doFilter. Whether there is a name cookie named autoLogin.

Always allow.

 

Public VoidDoFilter (ServletRequest request, ServletResponse response,

FilterChain chain)ThrowsIOException, ServletException {

// Read the cookie here

HttpServletRequest req = (HttpServletRequest) request;

// Obtain the cookie

Cookie [] cs = req. getCookies ();

If(Cs! =Null){

For(Cookie c: cs ){

If(C. getName (). equals ("autoLogin") {// If the cookie for Automatic Logon exists

String value = c. getValue (); // User Name

// Logon success means

Req. getSession (). setAttribute ("name", value );

Break;

}

}

}

// Whether or not automatic logon is

Chain. doFilter (request, response );

}

Fourth: configure all URLs in web. xml = /*

<Filter>

<Filter-name> auto </filter-name>

<Filter-class> cn. itcast. filter. AutoFilter </filter-class>

</Filter>

<Filter-mapping>

<Filter-name> auto </filter-name>

<Url-pattern>/* </url-pattern>

</Filter-mapping>

 

Step 5: Exit Development

System.Err. Println ("User logout ");

// Delete the entire session

Request. getSession (). invalidate ();

Cookie c =NewCookie ("autoLogin", "ddd ");

C. setMaxAge (0 );

C. setPath (request. getContextPath ());

Response. addCookie (c );

// Request. getSession (). removeAttribute ("name ");

Response. sendRedirect (request. getContextPath () + "/index. jsp ");

Step 6: optimize the code

During manual logon, the user enters the AutoFiilter doFilter method and reads all cookies once. This traversal is redundant for users.

Therefore, the LoginServet url should be ignored in doFiler.

And you cannot log on automatically when you exit.

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.