欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于resty?security的Api權(quán)限控制與事務(wù)支持

 更新時(shí)間:2022年03月07日 14:39:46   作者:dreampie  
這篇文章主要為大家介紹了基于resty?security的Api權(quán)限控制與事務(wù)支持讓數(shù)據(jù)操作處于事務(wù)控制下,有需要的朋友可以借鑒參考下,希望能夠有所幫助

讓數(shù)據(jù)操作處于事務(wù)控制下

1. 在Appconfig里配置事務(wù)攔截器

public void configInterceptor(InterceptorLoader interceptorLoader) {
    //事務(wù)的攔截器 @Transaction
    interceptorLoader.add(new TransactionInterceptor());
}

2. 在Resource的方法上使用Transaction注解配置事務(wù)

@API("/users")
public class UserResource extends ApiResource {
  /**
   * 在一個(gè)數(shù)據(jù)源執(zhí)行多個(gè)數(shù)據(jù)操作使用@Transaction注解
   * 如果時(shí)多個(gè)數(shù)據(jù)源 使用 @Transaction(name={"ds1","ds2"})
   * 數(shù)據(jù)源的名字和application.properties 里對(duì)應(yīng)
   */
  @POST
  @Transaction
  public User save(User user,UserInfo info) {
    return user.save() && info.save();
  }
}

 對(duì)Api進(jìn)行權(quán)限控制

1. 設(shè)計(jì)權(quán)限數(shù)據(jù)結(jié)構(gòu)

DROP TABLE IF EXISTS sec_user;
CREATE TABLE sec_user (
  id            BIGINT       NOT NULL AUTO_INCREMENT PRIMARY KEY,
  username      VARCHAR(50)  NOT NULL COMMENT '登錄名',
  providername  VARCHAR(50)  NOT NULL COMMENT '提供者',
  email         VARCHAR(200) COMMENT '郵箱',
  mobile        VARCHAR(50) COMMENT '手機(jī)',
  password      VARCHAR(200) NOT NULL COMMENT '密碼',
  avatar_url    VARCHAR(255) COMMENT '頭像',
  first_name    VARCHAR(10) COMMENT '名字',
  last_name     VARCHAR(10) COMMENT '姓氏',
  full_name     VARCHAR(20) COMMENT '全名',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP   NOT NULL,
  updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
  deleted_at TIMESTAMP NULL
) ENGINE =InnoDB DEFAULT CHARSET =utf8 COMMENT ='用戶';

DROP TABLE IF EXISTS sec_user_info;
CREATE TABLE sec_user_info (
  id          BIGINT    NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id     BIGINT    NOT NULL COMMENT '用戶id',
  creator_id  BIGINT COMMENT '創(chuàng)建者id',
  gender      INT DEFAULT 0 COMMENT '性別0男,1女',
  province_id BIGINT COMMENT '省id',
  city_id     BIGINT COMMENT '市id',
  county_id   BIGINT COMMENT '縣id',
  street      VARCHAR(500) COMMENT '街道',
  zip_code    VARCHAR(50) COMMENT '郵編',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP   NOT NULL,
  updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
  deleted_at TIMESTAMP NULL
) ENGINE =InnoDB DEFAULT CHARSET =utf8 COMMENT ='用戶信息';

DROP TABLE IF EXISTS sec_role;
CREATE TABLE sec_role (
  id         BIGINT    NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name       VARCHAR(50)   NOT NULL COMMENT '名稱',
  value      VARCHAR(50)  NOT NULL COMMENT '值',
  intro      VARCHAR(255) COMMENT '簡(jiǎn)介',
  pid        BIGINT DEFAULT 0 COMMENT '父級(jí)id',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP   NOT NULL,
  updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
  deleted_at TIMESTAMP NULL
) ENGINE =InnoDB DEFAULT CHARSET =utf8 COMMENT ='角色';

DROP TABLE IF EXISTS sec_user_role;
CREATE TABLE sec_user_role (
  id      BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id BIGINT NOT NULL,
  role_id BIGINT NOT NULL
) ENGINE =InnoDB DEFAULT CHARSET =utf8 COMMENT ='用戶角色';

DROP TABLE IF EXISTS sec_permission;
CREATE TABLE sec_permission (
  id         BIGINT      NOT NULL AUTO_INCREMENT PRIMARY KEY,
  name       VARCHAR(50) NOT NULL COMMENT '名稱',
  method      VARCHAR(10) NOT NULL COMMENT '方法',
  value      VARCHAR(50) NOT NULL COMMENT '值',
  url        VARCHAR(255) COMMENT 'url地址',
  intro      VARCHAR(255) COMMENT '簡(jiǎn)介',
  pid        BIGINT DEFAULT 0 COMMENT '父級(jí)id',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP   NOT NULL,
  updated_at TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
  deleted_at TIMESTAMP NULL
) ENGINE =InnoDB DEFAULT CHARSET =utf8 COMMENT ='權(quán)限';

DROP TABLE IF EXISTS sec_role_permission;
CREATE TABLE sec_role_permission (
  id            BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  role_id       BIGINT NOT NULL,
  permission_id BIGINT NOT NULL
) ENGINE =InnoDB DEFAULT CHARSET =utf8 COMMENT ='角色權(quán)限';

2. 實(shí)現(xiàn)AuthenticateService權(quán)限數(shù)據(jù)加載接口

public class MyAuthenticateService implements AuthenticateService {
  /**
   * 查詢用戶信息  
   * @param username 登錄的用戶名
   * @return 用戶權(quán)限對(duì)象
   */
  public Principal getPrincipal(String username) {
    Principal<User> principal=null;
    User u = User.dao.findBy("username=?", username);
    if (u != null) {
      principal = new Principal<User>(u.getStr("username"), u.getStr("password"), new HashSet<String>(u.getPermissions()), u);
    }
    return principal;
  }
  /**
   * 加載全部的權(quán)限信息
   * @return 權(quán)限集合
   */
  public Set<Credential> getAllCredentials() {
    List<Permission> permissions = Permission.dao.findBy("deleted_at is null");
    Set<Credential> credentials = new HashSet<Credential>();

    for (Permission permission : permissions) {
      credentials.add(new Credential(permission.getStr("method"), permission.getStr("url"), permission.getStr("value")));
    }

    return credentials;
  }
}

3. 在AppConfig里配置SecurityInterceptor權(quán)限攔截器

public void configInterceptor(InterceptorLoader interceptorLoader) {
    //權(quán)限攔截器 2表示用戶登錄的最大session數(shù)量 MyAuthenticateService 數(shù)據(jù)加載實(shí)現(xiàn)類
    interceptorLoader.add(new SecurityInterceptor(2, new MyAuthenticateService()));
}

4. 模擬的用戶數(shù)據(jù)

-- create role--
INSERT INTO sec_role(name, value, intro, pid,created_at)
VALUES ('超級(jí)管理員','R_ADMIN','',0, current_timestamp),
       ('銷售','R_SALER','',1,current_timestamp),
       ('財(cái)務(wù)','R_FINANCER','',1,current_timestamp),
       ('設(shè)置','R_SETTER','',1,current_timestamp);

-- create permission--
INSERT INTO sec_permission( name,method, value, url, intro,pid, created_at)
VALUES ('訂單','*','P_ORDER','/api/v1.0/orders/**','訂單訪問權(quán)限',0,current_timestamp),
       ('銷售','*','P_SALE','/api/v1.0/sales/**','銷售訪問權(quán)限',0,current_timestamp),
       ('財(cái)務(wù)','*','P_FINANCE','/api/v1.0/finances/**','財(cái)務(wù)訪問權(quán)限',0,current_timestamp),
       ('倉庫','*','P_STORE','/api/v1.0/stores/**','倉庫訪問權(quán)限',0,current_timestamp),
       ('設(shè)置','*','P_SETTING','/api/v1.0/settings/**','設(shè)置訪問權(quán)限',0,current_timestamp);

INSERT INTO sec_role_permission(role_id, permission_id)
VALUES (1,1),(1,2),(1,3),(1,4),(1,5),
       (2,1),(2,2),(2,4),
       (3,1),(3,2),(3,3),(3,4),
       (4,5);

-- user data--
-- create  admin--
INSERT INTO sec_user(username, providername, email, mobile, password, avatar_url, first_name, last_name, full_name, created_at)
VALUES ('admin','dreampie','<a  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"          target="_blank" >[email?protected]</a>','18611434500','a217d8ac340ee5da8098bff32a5769ebad5d4cfd74adebe6c7020db4dc4c3df517f56f6bc41882deb47814bd060db6f1e225219b095d7906d2115ba9e8ab80a0','','仁輝','王','仁輝·王',current_timestamp),
       ('saler','dreampie','<a  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"          target="_blank" >[email?protected]</a>','18611434500','a217d8ac340ee5da8098bff32a5769ebad5d4cfd74adebe6c7020db4dc4c3df517f56f6bc41882deb47814bd060db6f1e225219b095d7906d2115ba9e8ab80a0','','仁輝','王','仁輝·王',current_timestamp),
       ('financer','dreampie','<a  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"          target="_blank" >[email?protected]</a>','18611434500','a217d8ac340ee5da8098bff32a5769ebad5d4cfd74adebe6c7020db4dc4c3df517f56f6bc41882deb47814bd060db6f1e225219b095d7906d2115ba9e8ab80a0','','仁輝','王','仁輝·王',current_timestamp),
       ('setter','dreampie','<a  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"          target="_blank" >[email?protected]</a>','18611434500','a217d8ac340ee5da8098bff32a5769ebad5d4cfd74adebe6c7020db4dc4c3df517f56f6bc41882deb47814bd060db6f1e225219b095d7906d2115ba9e8ab80a0','','仁輝','王','仁輝·王',current_timestamp);

-- create user_info--
INSERT INTO sec_user_info(user_id, creator_id, gender,province_id,city_id,county_id,street,created_at)
VALUES (1,0,0,1,2,3,'人民大學(xué)',current_timestamp),
       (2,0,0,1,2,3,'人民大學(xué)',current_timestamp),
       (3,0,0,1,2,3,'人民大學(xué)',current_timestamp),
       (4,0,0,1,2,3,'人民大學(xué)',current_timestamp);

-- create user_role--
INSERT INTO sec_user_role( user_id, role_id)
VALUES (1,1),(2,2),(3,3),(4,4);

以上就是基于resty security的Api權(quán)限控制與事務(wù)支持的詳細(xì)內(nèi)容,更多關(guān)于resty security的Api權(quán)限控制與事務(wù)支持的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • JDK安裝配置教程

    JDK安裝配置教程

    這篇文章主要為大家詳細(xì)介紹了JDK安裝配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Maven中pom.xml文件報(bào)錯(cuò)的原因解決

    Maven中pom.xml文件報(bào)錯(cuò)的原因解決

    創(chuàng)建Maven項(xiàng)目的時(shí)候,如果你選擇的Packaging為war,那么就會(huì)報(bào)錯(cuò),本文主要介紹了Maven中pom.xml文件報(bào)錯(cuò)的原因解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Netty分布式ByteBuf中PooledByteBufAllocator剖析

    Netty分布式ByteBuf中PooledByteBufAllocator剖析

    這篇文章主要為大家介紹了Netty分布式ByteBuf剖析PooledByteBufAllocator簡(jiǎn)述,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • SpringBoot靜態(tài)資源路徑配置及主頁顯示

    SpringBoot靜態(tài)資源路徑配置及主頁顯示

    這篇文章主要介紹了SpringBoot靜態(tài)資源路徑配置及主頁顯示,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • java使用Apache工具集實(shí)現(xiàn)ftp文件傳輸代碼詳解

    java使用Apache工具集實(shí)現(xiàn)ftp文件傳輸代碼詳解

    這篇文章主要介紹了java使用Apache工具集實(shí)現(xiàn)ftp文件傳輸代碼詳解,分享了詳細(xì)連接ftp server和上傳文件,下載文件的代碼,以及結(jié)果展示,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • mybatis中實(shí)現(xiàn)枚舉自動(dòng)轉(zhuǎn)換方法詳解

    mybatis中實(shí)現(xiàn)枚舉自動(dòng)轉(zhuǎn)換方法詳解

    在使用mybatis的時(shí)候經(jīng)常會(huì)遇到枚舉類型的轉(zhuǎn)換,下面這篇文章主要給大家介紹了關(guān)于mybatis中實(shí)現(xiàn)枚舉自動(dòng)轉(zhuǎn)換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-08-08
  • 淺談Spring解決循環(huán)依賴的三種方式

    淺談Spring解決循環(huán)依賴的三種方式

    本篇文章主要介紹了淺談Spring循環(huán)依賴的三種方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • 淺談Java的Synchronized鎖原理和優(yōu)化

    淺談Java的Synchronized鎖原理和優(yōu)化

    這篇文章主要介紹了Java的Synchronized鎖原理和優(yōu)化,synchronized的作用是保證在同一時(shí)刻, 被修飾的代碼塊或方法只會(huì)有一個(gè)線程執(zhí)行,以達(dá)到保證并發(fā)安全的效果,需要的朋友可以參考下
    2023-05-05
  • mybatis創(chuàng)建項(xiàng)目報(bào)Invalid?bound?statement?(not?found)錯(cuò)誤解決方法

    mybatis創(chuàng)建項(xiàng)目報(bào)Invalid?bound?statement?(not?found)錯(cuò)誤解決方法

    使用MyBatis能夠幫助我們將SQL語句和Java代碼分離,這篇文章主要給大家介紹了關(guān)于mybatis創(chuàng)建項(xiàng)目報(bào)Invalid?bound?statement?(not?found)錯(cuò)誤的解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Java實(shí)現(xiàn)簡(jiǎn)易俄羅斯方塊

    Java實(shí)現(xiàn)簡(jiǎn)易俄羅斯方塊

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)易俄羅斯方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06

最新評(píng)論