最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?Security權限注解啟動及邏輯處理使用示例

 更新時間:2023年07月20日 08:57:09   作者:朱永勝  
這篇文章主要為大家介紹了Spring?Security權限注解啟動及邏輯處理使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

啟用注解

@EnableGlobalMethodSecurity(prePostEnabled = true)

正常啟用開啟那個注解就行,下面放下我的配置

package com.fedtech.sys.provider.config.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import javax.annotation.Resource;
/**
 * 資源配置
 *
 * @author <a href = "mailto:njpkhuan@gmail.com" > huan </a >
 * @date 2021/1/13
 * @since 1.0.0
 */
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Resource
    RedisConnectionFactory redisConnectionFactory;
    @Resource
    private TokenStore tokenStore;
    @Bean
    public TokenStore redisTokenStore() {
        return new RedisTokenStore(redisConnectionFactory);
    }
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.tokenStore(tokenStore);
    }
}

角色

/**
     * 查詢單個用戶
     *
     * @param query {@link UserQuery}
     *
     * @return com.fedtech.common.util.result.R<com.fedtech.sys.provider.view.UserView>
     *
     * @author <a href = "mailto:njpkhuan@gmail.com" > huan </a >
     * @date 2021/2/20
     * @since 1.0.0
     */
    @GetMapping("select")
    @PreAuthorize("hasAuthority('admin')")
    public R<UserView> selectUser(UserQuery query) {
        UserDto dto = userService.selectUser(query);
        return R.successWithData(userMapper.dto2View(dto));
    }

權限

默認的是DenyAllPermissionEvaluator,所有權限都拒絕,所以要自定義

自定義處理邏輯

我是把權限放到了自定義的userDetails里面

package com.fedtech.common.model;
import cn.hutool.core.collection.CollUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;
/**
 * 該類返回的是安全的,能夠提供給用戶看到的信息,即脫敏后的信息
 *
 * @author <a href = "mailto:njpkhuan@gmail.com" > huan </a >
 * @date 2021/1/9
 * @since 1.0.0
 */
@Data
@Slf4j
public class SecurityUser implements UserDetails {
    private static final long serialVersionUID = 8689435103879098852L;
    /**
     * 鹽
     */
    private String salt;
    /**
     * 用戶token
     */
    private String token;
    /**
     * 用戶狀態(tài)
     */
    private String status;
    /**
     * 用戶密碼
     */
    private String password;
    /**
     * 用戶登錄賬號
     */
    private String loginName;
    private Long userId;
    /**
     * 用戶角色
     *
     * @date 2021/1/10
     * @since 1.0.0
     */
    private List<UserRole> roleList;
    /**
     * 權限列表
     *
     * @date 2021/1/11
     * @since 1.0.0
     */
    private List<UserPermission> permissionList;
    /**
     * 客戶端用戶
     *
     * @param client 客戶端
     *
     * @author <a href = "mailto:njpkhuan@gmail.com" > huan </a >
     * @date 2021/1/13
     * @since 1.0.0
     */
    public SecurityUser(OauthClientDetails client) {
        if (client != null) {
            password = client.getClientSecret();
            loginName = client.getClientId();
            String authorities = client.getAuthorities();
            StringTokenizer stringTokenizer = new StringTokenizer(authorities, ", ");
            roleList = new ArrayList<>();
            if (stringTokenizer.hasMoreTokens()) {
                UserRole userRole = new UserRole();
                userRole.setCode(stringTokenizer.nextToken());
                roleList.add(userRole);
            }
        }
    }
    /**
     * 普通用戶
     *
     * @param user           用戶
     * @param roleList       角色
     * @param permissionList 權限
     *
     * @author <a href = "mailto:njpkhuan@gmail.com" > huan </a >
     * @date 2021/1/13
     * @since 1.0.0
     */
    public SecurityUser(User user, List<UserRole> roleList, List<UserPermission> permissionList) {
        if (user != null) {
            salt = user.getSalt();
            token = user.getToken();
            status = user.getStatus();
            password = user.getPassword();
            loginName = user.getLoginName();
            userId = user.getId();
            this.roleList = roleList;
            this.permissionList = permissionList;
        }
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        if (!CollUtil.isEmpty(roleList)) {
            for (UserRole role : roleList) {
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getCode());
                authorities.add(authority);
            }
        }
        log.debug("獲取到的用戶權限:{}", authorities);
        return authorities;
    }
    @Override
    public String getPassword() {
        return password;
    }
    @Override
    public String getUsername() {
        return loginName;
    }
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    @Override
    public boolean isEnabled() {
        return true;
    }
}
package com.fedtech.common.config;
import cn.hutool.core.collection.CollUtil;
import com.fedtech.common.model.SecurityUser;
import com.fedtech.common.model.UserPermission;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication;
import java.io.Serializable;
import java.util.List;
/**
 * 自定義權限處理
 *
 * @author <a href="mailto:njpkhuan@gmail.com" rel="external nofollow" >huan</a>
 * @version 1.0.0
 * @date 2021/2/26
 */
@Slf4j
@Configuration
public class MyPermissionEvaluator implements PermissionEvaluator {
    @Override
    public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
        SecurityUser principal = (SecurityUser) authentication.getPrincipal();
        List<UserPermission> permissionList = principal.getPermissionList();
        if (CollUtil.isNotEmpty(permissionList)) {
            return permissionList.stream().anyMatch(x -> StringUtils.equals(x.getUrl(), (CharSequence) targetDomainObject) &&
                    StringUtils.equals(x.getCode(), (CharSequence) permission));
        }
        return false;
    }
    @Override
    public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
        return false;
    }
}

使用

/**
     * 查詢單個用戶
     *
     * @param query {@link UserQuery}
     *
     * @return com.fedtech.common.util.result.R<com.fedtech.sys.provider.view.UserView>
     *
     * @author <a href = "mailto:njpkhuan@gmail.com" > huan </a >
     * @date 2021/2/20
     * @since 1.0.0
     */
    @GetMapping("select")
    @PreAuthorize("hasPermission('/sys/user/insert','userInsert')")
    public R<UserView> selectUser(UserQuery query) {
        UserDto dto = userService.selectUser(query);
        return R.successWithData(userMapper.dto2View(dto));
    }

以上就是Spring Security權限注解啟動及邏輯處理使用示例的詳細內容,更多關于Spring Security權限注解的資料請關注腳本之家其它相關文章!

相關文章

  • Java中Flux類響應式編程的核心組件詳解

    Java中Flux類響應式編程的核心組件詳解

    Flux是響應式編程核心組件,支持異步流處理、背壓控制與豐富操作符,適用于Web應用、數(shù)據(jù)管道及事件流處理,與Mono的區(qū)別在于可發(fā)射0-N元素,適合多元素場景,本文給大家介紹Java中Flux類響應式編程的核心組件,感興趣的朋友跟隨小編一起看看吧
    2025-09-09
  • 使用FeignClient調用遠程服務時整合本地的實現(xiàn)方法

    使用FeignClient調用遠程服務時整合本地的實現(xiàn)方法

    這篇文章主要介紹了使用FeignClient調用遠程服務時整合本地的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 淺談synchronized方法對非synchronized方法的影響

    淺談synchronized方法對非synchronized方法的影響

    下面小編就為大家?guī)硪黄獪\談synchronized方法對非synchronized方法的影響。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Spring AI對接大模型開發(fā)易錯點總結與實戰(zhàn)解決辦法

    Spring AI對接大模型開發(fā)易錯點總結與實戰(zhàn)解決辦法

    本文介紹了SpringAI接入大模型時常見的問題及解決方案,主要從地址配置、密鑰鑒權、模型匹配、版本依賴四大維度入手,詳細分析了各種問題的成因,并提供了實用的配置與代碼解決方案,幫助開發(fā)者快速避坑,提高開發(fā)效率,需要的朋友可以參考下
    2026-05-05
  • 在Android中使用WebView在線查看PDF文件的方法示例

    在Android中使用WebView在線查看PDF文件的方法示例

    在Android應用開發(fā)中,有時我們需要在客戶端展示PDF文件,以便用戶可以閱讀或交互,這篇文章主要介紹了在Android中使用WebView在線查看PDF文件的方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-09-09
  • JDK21中虛擬線程到底是什么以及用法總結(看完便知)

    JDK21中虛擬線程到底是什么以及用法總結(看完便知)

    這篇文章主要給大家介紹了關于JDK21中虛擬線程到底是什么以及用法的相關資料,虛擬線程是一種輕量化的線程封裝,由jvm直接調度和管理,反之普通的線程其實是調用的操作系統(tǒng)的能力,對應的是操作系統(tǒng)級的線程,需要的朋友可以參考下
    2023-12-12
  • 使用java實現(xiàn)銀行家算法

    使用java實現(xiàn)銀行家算法

    這篇文章主要為大家詳細介紹了如何使用java實現(xiàn)銀行家算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • spring通過導入jar包和配置xml文件啟動的步驟詳解

    spring通過導入jar包和配置xml文件啟動的步驟詳解

    這篇文章主要介紹了spring通過導入jar包和配置xml文件啟動,本文分步驟通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • java數(shù)據(jù)結構與算法之冒泡排序詳解

    java數(shù)據(jù)結構與算法之冒泡排序詳解

    這篇文章主要介紹了java數(shù)據(jù)結構與算法之冒泡排序,結合實例形式詳細分析了java冒泡排序的原理、實現(xiàn)技巧與相關注意事項,需要的朋友可以參考下
    2017-05-05
  • Java運行時環(huán)境之ClassLoader類加載機制詳解

    Java運行時環(huán)境之ClassLoader類加載機制詳解

    這篇文章主要給大家介紹了關于Java運行時環(huán)境之ClassLoader類加載機制的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-01-01

最新評論

黎城县| 大田县| 五台县| 奉贤区| 彭阳县| 黄浦区| 沾益县| 五常市| 梨树县| 成武县| 东山县| 姜堰市| 濮阳县| 宁波市| 余干县| 临清市| 新竹县| 阿勒泰市| 潮州市| 淮滨县| 南召县| 开远市| 邛崃市| 英吉沙县| 湖南省| 重庆市| 吉林省| 新丰县| 清丰县| 宁都县| 奇台县| 平塘县| 保康县| 全椒县| 安福县| 汾阳市| 荣昌县| 商都县| 大余县| 房山区| 镇安县|