Home  >  Article  >  Java  >  Authentication Java

Authentication Java

PHPz
PHPzOriginal
2024-08-30 15:58:38458browse

Authentication java is a term of the security to identity confirmation of web applications. It is a function to confirm user identification of the websites & web applications using a programming language. It confirms the users’ use and permits them to access the website, application, and software-related products using Java technology. It is a security method to identify the authorized user and give permission to use the application using the security terms of the Java language.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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 team members.

Syntax

This syntax is used to authenticate the user’s particular branch, such as student, teacher, non-teaching staff, and principal. You can use a username, email id, and password to log in 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("test@123").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 work in Java?

Use web application with security and login form. This form redirects to the 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 a 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 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 ("test@123")
.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:

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 ("school@123")
.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

User name:

Output:

Authentication Java

Output

Authentication Java

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 are 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 ("test@123")
.roles ("student"))
.withUser(users.username("sam")
.password("exam@123")
.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

User:
Teachrs portal

Output:

Authentication Java

Output:

Authentication Java

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 are 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 ("test@123")
.roles ("student"))
.withUser(users.username("sam")
.password("exam@123")
.roles("student", "teacher"))
.withUser(users.username("Ram")
.password("admin@123")
.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

User:
Teachers portal
Principle portal

Output:

Authentication Java

Output:

Authentication Java

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

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 authorities. It becomes easy, attractive, user-friendly, and elegant websites and web applications. This function sorts the documentation per the user’s identity and returns only the required data. It helps to get complicated information easily without disturbing others’ privacy.

The above is the detailed content of Authentication Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Counting sort in javaNext article:Counting sort in java