Basic login example
In this section, we're going to write a simple example to show how things work in the new security API. Our example will be about a web page (servlet) that should be secured. A user should log in by providing a valid username and a password to be able to access the mentioned page.
In the new security API, this will be done as follows:
- Creating a web page to protect
- Mentioning who are allowed to access the web page
- Defining users and associated roles in a database
- Mapping security configuration to the user database
Creating a web page to protect
Let's start by creating a simple servlet that just welcomes the user to their home:
@WebServlet("/home")
public class HomeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse
resp)
throws ServletException, IOException {
resp.getOutputStream().println("Welcome to your home!");
}
} Now, just try to access the resource by browsing /sec/home....