首頁  >  文章  >  Java  >  認證Java

認證Java

PHPz
PHPz原創
2024-08-30 15:58:38460瀏覽

Authentication java 是 Web 應用程式身分確認的安全術語。它是使用程式語言確認網站和網路應用程式的用戶身份的功能。它確認使用者的使用並允許他們使用 Java 技術存取網站、​​應用程式和軟體相關產品。它是一種安全方法,用於使用 Java 語言的安全條款來識別授權使用者並授予使用應用程式的權限。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

它是一個客戶端和伺服器端功能,使用唯一的內容並透過安全密碼和使用者身分進行確認。它在客戶端使用使用者id和密碼,並使用Java程式語言以真實身分存取伺服器端資料。這是一個保持 Web 應用程式安全並僅使用可存取的團隊成員的文件流程。

文法

此語法用於驗證使用者的特定分支,例如學生、教師、非教學人員和校長。您可以使用使用者名稱、電子郵件地址和密碼登入並確認身分。

在此語法中,應用程式使用使用者名稱和密碼進行身份驗證。

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();
}
}

Java 中的身份驗證如何運作?

使用具有安全性和登入表單的網路應用程式。此表單會重新導向至 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>

使用網頁應用程式對登入表單進行身份驗證。此表單會重新導向至 JSP 頁面。

<p>
User: <security:authentication property = "principal.username" />
</p>

使用 java spring 框架使用 Java 驗證語法。 Java使用Spring security來進行權限認證。

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();
}
}

Java 驗證範例

以下是範例:

範例#1

基本範例如下圖所示。

代碼:

檔案: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();
}
}

檔案: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:

輸出:

認證Java

輸出

認證Java

說明:

  • 在這裡,您可以看到單一使用者名稱中的單一使用者身份驗證。
  • 「Sunny」僅透過 Java 驗證存取學生入口網站。
  • 您將獲得單一真實使用者的單一表格。

範例#2

Java 範例中的兩個驗證和輸出如下所示。

代碼:

檔案: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();
}
}

檔案: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

輸出:

認證Java

輸出:

認證Java

說明:

  • 在這裡,您可以在一個使用者名稱中看到兩個身份驗證。
  • 「sam」透過 Java 身份驗證存取教師和學生入口網站。
  • 您將獲得多個真實使用者的單一表單。

範例#3

Java 範例中的多重驗證和輸出如下所示。

代碼:

檔案: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();
}
}

檔案: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

輸出:

認證Java

輸出:

認證Java

說明:

  • 在這裡,您可以看到單一使用者名稱中的多個身份驗證。
  • 「Ram」透過 Java 身份驗證存取教師、學生和管理入口網站。
  • 您將獲得多個真實使用者的單一表格。

結論

Java 中的驗證提供資料和權限的安全性、安全性和隱私性。身份驗證用於存取相應使用者和權限的資料庫的一部分。它變得簡單、有吸引力、用戶友好且優雅的網站和 Web 應用程式。此函數根據使用者的身分對文件進行排序,並僅傳回所需的資料。它有助於輕鬆獲得複雜的訊息,同時又不會侵犯他人的隱私。

以上是認證Java的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn