Introduction to Authentication Java
Authentication java is a term of the security to identity confirmation of the web applications. It is a function to confirm user identification of the websites & web applications using a programming language. It is confirming the use of the users and permits them to access the website, application, and software-related products using java technology. It is a security method to identifying the authorized user and give permission to use the application using security terms of the java language.
It is a client and server-side function to use unique content and confirm with a security password and user identity. It is used the user id and password on the client-side and accesses the server-side data with true identification using a java programming language. It is a documentation process to keep a secure web application and use only accessible members of the team.
Syntax
This syntax is used to an authentic particular branch of the user such as student, teacher, non-teaching staff, and principal. You can use username, email id, and password to login and confirm identification.
In this syntax, the application uses a username and password for authentication.
public class AppSecurityConfig extends AppSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder authentic) throws Exception {
UserBuilder userid = User.withDefaultPasswordEncoder();
authentic.inMemoryAuthentication()
.withUser(usersid.username("merry").password("[email protected]").roles("student"))
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").hasRole("student")
.and()
.formLogin()
.loginPage("/useLoginPage")
.loginProcessingUrl("/authenticatationUser")
.permitAll()
.and()
.logout().permitAll();
}
}
How does Authentication works in Java?
Use web application with security and login form. This form redirect with jsp page.
<form:form action="${pageContext.request.contextPath}/authenticateUser"
Method = "POST">
<c:if test = "${param.error ! = null}">
<b class="failed"> username/password does not authenticate here… </b>
</c:if>
<p>
User ID: <input type = "text" name = "name" />
</p>
<p>
Password: <input type = "password" name = "pswrd" />
</p>
<input type = "submit" value = "Submit" />
</form:form>
Use web application for authentication of the login form. This form redirects to the JSP page.
<p>
User: <security:authentication property = "principal.username" />
</p>
Use java authentication syntax using java spring frameworks. Java uses the spring security to authenticate the authority.
public class AppSecurityConfig extends AppSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder authentic) throws Exception {
UserBuilder userid = User.withDefaultPasswordEncoder();
authentic.inMemoryAuthentication()
.withUser (usersid.username ("merry")
.password ("[email protected]")
.roles ("student"))
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/")
.hasRole("student")
.and()
.formLogin()
.loginPage("/useLoginPage")
.loginProcessingUrl("/authenticatationUser")
.permitAll()
.and()
.logout().permitAll();
}
}
Examples of Authentication Java
Given below are the examples mentioned:
Example #1
The basic example is shown below.
Code:
File: authenticationApp.java
public class authenticationApp extends AppSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder authentic) throws Exception {
UserBuilder userid = User.withDefaultPasswordEncoder();
authentic.inMemoryAuthentication()
.withUser (usersid.username ("sunny")
.password ("[email protected]")
.roles ("student"))
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/")
.hasRole("student")
.and()
.formLogin()
.loginPage("/useLoginPage")
.loginProcessingUrl("/authenticatationUser")
.permitAll()
.and()
.logout().permitAll();
}
}
File: main_login.jsp
<form:form action="${pageContext.request.contextPath}/authenticateUser"
Method = "POST">
<c:if test = "${param.error ! = null}">
<b class="failed"> username/password does not authenticate here… </b>
</c:if>
<p>
User ID: <input type = "text" name = "name" />
</p>
<p>
Password: <input type = "password" name = "pswrd" />
</p>
<input type = "submit" value = "Submit" />
</form:form>
File: authentication.jsp
<p>
User name: <security:authentication property = "principal.username" />
</p>
Output:
Output
Explanation:
- Here, you see single user authentication in a single user name.
- The “Sunny” accesses only the student portal with java authentication.
- You get the single form for a single authentic user.
Example #2
Two authentications in the java example and output is shown below.
Code:
File: authenticationApp.java
public class authenticationApp extends AppSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder authentic) throws Exception {
UserBuilder userid = User.withDefaultPasswordEncoder();
authentic.inMemoryAuthentication()
.withUser (usersid.username ("merry")
.password ("[email protected]")
.roles ("student"))
.withUser(users.username("sam")
.password("[email protected]")
.roles("student", "teacher"))
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").hasRole("student")
.antMatchers("/teachers/**").hasRole("teacher")
.and()
.formLogin()
.loginPage("/useLoginPage")
.loginProcessingUrl("/authenticatationUser")
.permitAll()
.and()
.logout().permitAll();
}
}
File: main_login.jsp
<form:form action="${pageContext.request.contextPath}/authenticateUser"
Method = "POST">
<c:if test = "${param.error ! = null}">
<b class="failed"> username/password does not authenticate here… </b>
</c:if>
<p>
User ID: <input type = "text" name = "name" />
</p>
<p>
Password: <input type = "password" name = "pswrd" />
</p>
<input type = "submit" value = "Submit" />
</form:form>
File: authentication.jsp
<p>
User: <security:authentication property = "principal.username" />
<br>
<a href="${pageContext.request.contextPath}/teacher"> Teachrs portal </a> </p>
Output:
Output:
Explanation:
- Here, you see two authentications in a single user name.
- The “sam” accesses the teacher and student portal with java authentication.
- You get the single form for multiple authentic users.
Example #3
Multiple authentications in the java example and output is shown below.
Code:
File: authenticationApp.java
public class authenticationApp extends AppSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder authentic) throws Exception {
UserBuilder userid = User.withDefaultPasswordEncoder();
authentic.inMemoryAuthentication()
.withUser (usersid.username ("merry")
.password ("[email protected]")
.roles ("student"))
.withUser(users.username("sam")
.password("[email protected]")
.roles("student", "teacher"))
.withUser(users.username("Ram")
.password("[email protected]")
.roles("student", "teacher", "principle"))
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/")
.hasRole("student")
.antMatchers("/teachers/**").hasRole("teacher")
.antMatchers("/principles/**").hasRole("principle")
.and()
.formLogin()
.loginPage("/useLoginPage")
.loginProcessingUrl("/authenticatationUser")
.permitAll()
.and()
.logout().permitAll();
}
}
File: main_login.jsp
<form:form action="${pageContext.request.contextPath}/authenticateUser"
Method = "POST">
<c:if test = "${param.error ! = null}">
<b class="failed"> username/password does not authenticate here… </b>
</c:if>
<p>
User ID: <input type = "text" name = "name" />
</p>
<p>
Password: <input type = "password" name = "pswrd" />
</p>
<input type = "submit" value = "Submit" />
</form:form>
File: authentication.jsp
<p>
User: <security:authentication property = "principal.username" />
<br>
<a href="${pageContext.request.contextPath}/teacher"> Teachers portal </a>
<br>
<a href="${pageContext.request.contextPath}/principle"> Principle portal </a>
</p>
Output:
Output:
Explanation:
- Here, you see multiple authentications in a single user name.
- The “Ram” accesses the teacher, student, and admin portal with java authentication.
- You get the single form for multiple authentic users.
Conclusion
The authentication in java provides security, safety, and privacy of the data and authority. The authentication uses for accessing part of the database to respective users and authority. It becomes easy, attractive, user-friendly, and elegant websites and web applications. This function sorted the documentation as per the user’s identity and return only the required data. It helps to get complicated information easily without disturbing other’s privacy.
Recommended Articles
This is a guide to Authentication Java. Here we discuss the introduction, how does authentication work in Java? and examples respectively. You may also have a look at the following articles to learn more –