<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 使用`@PreAuthorize`和`@Secured`的 Spring 方法安全性 > 原文: [https://howtodoinjava.com/spring-security/spring-3-method-level-security-example-using-preauthorize-and-secured/](https://howtodoinjava.com/spring-security/spring-3-method-level-security-example-using-preauthorize-and-secured/) Spring 框架使保護您的應用程序變得非常容易,您只需要正確地使用一些基本配置即可! 此安全性可以應用于 Web 應用程序中的多個級別。 Spring 對這些級別的基本支持: * 網址級別的安全性 * 方法級別的安全性 * 實體或對象級別的安全性 在此 Spring Security 教程中,學習使用諸如`@PreAuthorize`和`@Secured`之類的注解來應用方法安全性。 ## 啟用`@Secured`和`@PreAuthorize` 方法級別安全性的核心是配置元素`<global-method-security/>`。 這需要在 spring 的配置文件中定義。 此元素用于在應用程序中啟用基于注解的安全性(通過在元素上設置適當的屬性)。 您只應聲明一個`<global-method-security/>`元素。 例如 ```java <global-method-security pre-post-annotations="enabled" /> ``` 以上配置將在代碼中啟用 [`@PreAuthorize`](http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/access/prepost/PreAuthorize.html) 和 [`@PostAuthorize`](http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/access/prepost/PostAuthorize.html) 注解。 //要么 上述配置的另一種變化是: ```java <global-method-security secured-annotations="enabled" /> ``` 這將在您的代碼中啟用 [`@Secured`](http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/access/annotation/Secured.html) 注解。 這些注解采用一個字符串參數,該參數可以是`role-name`或**表達式**,并且取決于您對`<http>`元素的`use-expression`值的配置使用。 如果`use-expression`設置為 true,則應在注解內使用表達式,否則應直接使用角色名稱。 如果您需要定義簡單的規則,而不是根據用戶的權限列表檢查角色名稱,則基于表達式的注解是一個不錯的選擇。 您可以在同一應用程序中啟用一種以上類型的注解,但應避免在同一接口或類中混合使用多種注解類型,以免造成混淆。 ## 測試安全性注解 為了在運行的應用程序中測試以上注解,我使用的是先前教程的代碼庫,該代碼庫與[**基于登錄表單的安全性**](https://howtodoinjava.com/spring/spring-security/login-form-based-spring-3-security-example/)相關。 此應用程序已經過 URL 級別安全保護。 現在,我們還將添加對方法級別安全性的支持。 #### 修改`application-security.xml`配置 為了啟用對**方法級別安全性**的支持,我將使用`<global-method-security>`標記更新`application-security.xml`文件,如下所示: `application-security.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd"> <global-method-security pre-post-annotations="enabled" /> <http auto-config="false" use-expressions="true"> <intercept-url pattern="/login" access="permitAll" /> <intercept-url pattern="/logout" access="permitAll" /> <intercept-url pattern="/accessdenied" access="permitAll" /> <intercept-url pattern="/**" access="hasRole('ROLE_USER')" /> <form-login login-page="/login" default-target-url="/list" authentication-failure-url="/accessdenied" /> <logout logout-success-url="/logout" /> </http> <authentication-manager alias="authenticationManager"> <authentication-provider> <user-service> <user name="lokesh" password="password" authorities="ROLE_USER" /> <user name="admin" password="password" authorities="ROLE_USER,ROLE_ADMIN" /> </user-service> </authentication-provider> </authentication-manager> <beans:bean id="employeeDAO" class="com.howtodoinjava.dao.EmployeeDaoImpl" /> <beans:bean id="employeeManager" class="com.howtodoinjava.service.EmployeeManagerImpl" /> </beans:beans> ``` 其余所有代碼與以前的教程相同。 ## 注解方法以確保安全 在本教程中,我希望具有角色`admin`的用戶只能將員工添加到員工集合。 像以前一樣允許其他操作。 為此,我將在`EmployeeDaoImpl.java`中注解`add`方法,如下所示: `EmployeeDaoImpl.java` ```java package com.howtodoinjava.dao; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Repository; import com.howtodoinjava.entity.EmployeeEntity; @Repository public class EmployeeDaoImpl implements EmployeeDAO { @Autowired private SessionFactory sessionFactory; @PreAuthorize("hasRole('ROLE_ADMIN')") @Override public void addEmployee(EmployeeEntity employee) { //System.out.println(((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getAuthorities()); this.sessionFactory.getCurrentSession().save(employee); } @SuppressWarnings("unchecked") @Override public List<EmployeeEntity> getAllEmployees() { return this.sessionFactory.getCurrentSession().createQuery("from Employee").list(); } @Override public void deleteEmployee(Integer employeeId) { EmployeeEntity employee = (EmployeeEntity) sessionFactory.getCurrentSession().load( EmployeeEntity.class, employeeId); if (null != employee) { this.sessionFactory.getCurrentSession().delete(employee); } } } ``` `@PreAuthorize`注解將測試使用的登錄帳戶是否具有“ `ROLE_ADMIN`”授權。 如果用戶沒有此權限,將拋出拒??絕訪問的異常。 #### 示例 我們的應用程序已配置完畢,可以部署了。 所以,讓我們做吧! **1)在瀏覽器窗口**中點擊網址“ `http://localhost:8080/Spring3HibernateIntegration/login`” [![Spring-3-security-login-window](https://img.kancloud.cn/b8/09/b809bf08bc72f8d0138033850ec80964_436x175.jpg)](https://howtodoinjava.files.wordpress.com/2013/04/spring-3-security-login-window.png) 登錄窗口 由于所有 URL 均受保護,因此將出現一個登錄窗口。 **2)使用用戶名“ `lokesh`”和密碼“ `password`”登錄,然后嘗試添加員工** [![Access-denied](https://img.kancloud.cn/ce/2c/ce2c824cbdbedccbf03a7e3bd271d623_585x192.jpg)](https://howtodoinjava.files.wordpress.com/2013/04/access-denied.png) 拒絕訪問消息 由于`lokesh`沒有管理員權限,將引發拒絕訪問的異常。 **3)使用用戶名“`admin`”和密碼“`password`”登錄,然后嘗試添加員工** [![Employee management screen](https://img.kancloud.cn/06/e9/06e9babbc586b97be3f515200825fad0_346x361.jpg)](https://howtodoinjava.files.wordpress.com/2013/04/employee-management-screen.png) 員工管理界面 管理員可以添加員工,因為已為其分配了“`ROLE_ADMIN`”。 如果您在運行該應用程序時遇到任何問題,請告訴我。 學習愉快! [下載源碼](https://docs.google.com/file/d/0B7yo2HclmjI4czRJWWRucFBtdkU/edit?usp=sharing)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看