Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java servlet and authentication

I have a small application with 3-4 servlets and a basic module that provide me authentication like:

public class Authentication {
    public boolean isUserAuthenticated(){
        ....
    }
}

Is there a way to check the authentication using my class BEFORE every other servlet calls, without have to add code in each of them? I'd like to avoid the check of the user for every servlet I have and for every servlet I will have to add.

Any suggestion is well accepted :)

Thanks, Roberto

like image 843
Roberto Avatar asked Dec 07 '25 08:12

Roberto


1 Answers

Absolutely, use a servlet filter. It's the standard way of implementing security in Java Web applications.

The Java Servlet specification version 2.3 introduces a new component type, called a filter. A filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses. Filters typically do not themselves create responses, but instead provide universal functions that can be "attached" to any type of servlet or JSP page.

like image 199
cletus Avatar answered Dec 09 '25 21:12

cletus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!