Spring Security(7)

您好,我是湘王,這是我的博客園,歡迎您來,歡迎您再來~

 

有時某些業務或者功能,需要在用戶請求到來之前就進行一些判斷或執行某些動作,就像在Servlet中的FilterChain過濾器所做的那樣,Spring Security也有類似機制。Spring Security有三種增加過濾器的方式:addFilterBefaore()、 addFilterAt()和addFilterAfter(),也可以disable掉默認的過濾器,例如:

1、http.logout().disable();或者http.headers().disable();

2、用自定義過濾器http.addFilterAt(new MyLogoutFilter(), LogoutFilter.class)替換

Spring Security的官方列出了過濾器調用順序,具體可參考官方網站。

 

Spring Security已經定義好,可以直接使用的過濾器有下面這些:

 

 

 

比如,現在的互聯網應用都有一個通用的「業務規則」是:在執行所有功能接口的時候都要檢查確認接口簽名的有效性。所謂接口簽名其實就是一套進入準則,客戶端按照服務器規定的方式向服務器證明自己確實是某個網站的用戶。

那麼,Spring Security裏面可以這麼干:

/**
 * 自定義攔截過濾器
 *
 * @author 湘王
 */
@Component
public class CustomInterceptorFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException {
        // 保存參數=參數值對
        Map<String, String> mapResult = new HashMap<String, String>();
        // 請求中的參數
        Enumeration<String> em = request.getParameterNames();
        while (em.hasMoreElements()) {
            String paramName = em.nextElement();
            String value = request.getParameter(paramName);
            mapResult.put(paramName, value);
        }
        // 驗證參數,只要有一個不滿足條件,立即返回
        if (null == mapResult.get("platform") ||
            null == mapResult.get("timestamp") ||
            null == mapResult.get("signature")) {
                response.getWriter().write("api validate failure");
        }
        Object result = null;
        String platform = mapResult.get("platform");
        String timestamp = mapResult.get("timestamp");
        String signature = mapResult.get("signature");
        // 後端生成簽名:platform = "xiangwang" timestamp = "159123456789" signature = ""
        String sign = DigestUtils.md5DigestAsHex((platform + timestamp).getBytes());
        validateSignature(signature, sign, request, response, chain);
    }

    // 驗證簽名
    private void validateSignature(String signature, String sign, ServletRequest request,
                                   ServletResponse response, FilterChain chain) throws IOException {
        if (signature.equalsIgnoreCase(sign)) {
            try {
                // 讓調用鏈繼續往下執行
                chain.doFilter(request, response);
            } catch (Exception e) {
                response.getWriter().write("api validate failure");
            }
        } else {
            response.getWriter().write("api validate failure");
        }
    }

    public static void main(String[] args) {
        // 這裡的驗證簽名算法可以隨便自定義實現(guid = "0" platform = "web" timestamp = 156789012345)
        // 下面的代碼只是偽代碼,舉個例子而已
        String sign = "";
        if(StringUtils.isBlank(guid)) {
            // 首次登錄,後端 platform + timestamp + "xiangwang" 生成簽名
            sign = DigestUtils.md5DigestAsHex((platform + timestamp + "xiangwang").getBytes());
            validateSignature(signature, sign, request, response, chain);
        } else {
            // 不是首次登錄,後端 guid + platform + timestamp 生成簽名
            // 從Redis拿到token,這裡不實現
            // Object object = service.getObject("token#" + guid);
            Object object = "1234567890abcdefghijklmnopqrstuvwxyz";
            if(null == object) {
                response.getWrite().write("token expired");
            } else {
                token = (String) obejct;
                // 驗證sign
                sign = DigestUtils.md5DigestAsHex((platform + timestamp + "xiangwang").getBytes());
                validateSignature(signature, sign, request, response, chain);
            }
        }
        System.out.println(DigestUtils.md5DigestAsHex(("web" + "156789012345").getBytes()));
    }
}

 

 

然後修改WebSecurityConfiguration,加入剛才自定義的「過濾器」:

// 控制邏輯
@Override
protected void configure(HttpSecurity http) throws Exception {
    // 執行UsernamePasswordAuthenticationFilter之前添加攔截過濾
    http.addFilterBefore(new CustomInterceptorFilter(), UsernamePasswordAuthenticationFilter.class);

    http.authorizeRequests()
            .anyRequest().authenticated()
            // 設置自定義認證成功、失敗及登出處理器
            .and().formLogin().loginPage("/login")
            .successHandler(successHandler).failureHandler(failureHandler).permitAll()
            .and().logout().logoutUrl("/logout").deleteCookies("JSESSIONID")
            .logoutSuccessHandler(logoutSuccessHandler).permitAll()
            // 配置無權訪問的自定義處理器
            .and().exceptionHandling().accessDeniedHandler(accessDeniedHandler)
            // 記住我
            .and().rememberMe()
            // 數據庫保存,這種方式在關閉服務之後仍然有效
            .tokenRepository(persistentTokenRepository())
            // 默認的失效時間會從用戶最後一次操作開始計算過期時間,過期時間最小值就是60秒,
            // 如果設置的值小於60秒,也會被更改為60秒
            .tokenValiditySeconds(30 * 24 * 60 * 60)
            .userDetailsService(customUserDetailsService)
            .and().cors().and().csrf().disable();
}

 

 

運行postman測試後的效果為:

 

 

 

增加了接口需要的簽名參數。

 

在前面的內容中,幾乎沒有對Spring Security真正的核心功能,也就是認證授權做什麼說明,也只是簡單演示了一些admin角色登錄。那麼在做完前面這些鋪墊之後,就需要接着來說說這一塊了。

先創建創建sys_permission表,為認證授權的細化做準備:

 

  

同樣,需要創建實體類和Service類。

/**
 * 權限entity
 *
 * @author 湘王
 */
public class SysPermission implements Serializable, RowMapper<SysPermission> {
    private static final long serialVersionUID = 4121559180789799491L;

    private int id;
    private int roleid;
    private String path;
    private String permission;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    protected Date createtime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    protected Date updatetime;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getRoleid() {
        return roleid;
    }

    public void setRoleid(int roleid) {
        this.roleid = roleid;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public Date getUpdatetime() {
        return updatetime;
    }

    public void setUpdatetime(Date updatetime) {
        this.updatetime = updatetime;
    }

    @Override
    public SysPermission mapRow(ResultSet result, int i) throws SQLException {
        SysPermission permission = new SysPermission();

        permission.setId(result.getInt("id"));
        permission.setRoleid(result.getInt("roleid"));
        permission.setPath(result.getString("path"));
        permission.setPermission(result.getString("permission"));
        permission.setCreatetime(result.getTimestamp("createtime"));
        permission.setUpdatetime(result.getTimestamp("updatetime"));

        return permission;
    }
}

 

/**
 * 權限Service
 *
 * @author 湘王
 */
@Service
public class PermissionService {
    @Autowired
    private MySQLDao mySQLDao;

    // 得到某個角色的全部權限
    public List<SysPermission> getByRoleId(int roleid) {
        String sql = "SELECT id, url, roleid, permission, createtime, updatetime FROM sys_permission WHERE roleid = ?";
        return mySQLDao.find(sql, new SysPermission(), roleid);
    }
}

 

 

再在LoginController中增加幾個hasPermission()方法:

// 細化權限
@GetMapping("/admin/create")
@PreAuthorize("hasPermission('/admin', 'create')")
public String adminCreate() {
    return "admin有ROLE_ADMIN角色的create權限";
}

@GetMapping("/admin/read")
@PreAuthorize("hasPermission('/admin', 'read')")
public String adminRead() {
    return "admin有ROLE_ADMIN角色的read權限";
}

@GetMapping("/manager/create")
@PreAuthorize("hasPermission('/manager', 'create')")
public String managerCreate() {
    return "manager有ROLE_MANAGER角色的create權限";
}

@GetMapping("/manager/remove")
@PreAuthorize("hasPermission('/manager', 'remove')")
public String managerRemove() {
    return "manager有ROLE_MANAGER角色的remove權限";
}

 

 

再來實現對hasPermission()方法的處理,也就是自定義權限處理的過濾器:

/**
 * 自定義權限處理
 *
 * @author 湘王
 */
@Component
public class CustomPermissionEvaluator implements PermissionEvaluator {
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;

    @Override
    public boolean hasPermission(Authentication authentication, Object targetUrl, Object permission) {
        // 獲得loadUserByUsername()方法的結果
        User user = (User) authentication.getPrincipal();
        // 獲得用戶授權
        Collection<GrantedAuthority> authorities = user.getAuthorities();

        // 遍歷用戶所有角色
        for(GrantedAuthority authority : authorities) {
            String roleName = authority.getAuthority();
            int roleid = roleService.getByName(roleName).getId();
            // 得到角色所有的權限
            List<SysPermission> permissionList = permissionService.getByRoleId(roleid);
            if (null == permissionList) {
                continue;
            }

            // 遍歷permissionList
            for(SysPermission sysPermission : permissionList) {
                String pstr = sysPermission.getPermission();
                String path = sysPermission.getPath();
                // 判空
                if (StringUtils.isBlank(pstr) || StringUtils.isBlank(path)) {
                    continue;
                }
                // 如果訪問的url和權限相符,返回true
                if (path.equals(targetUrl) && pstr.equals(permission)) {
                    return true;
                }
            }
        }

        return false;
    }

    @Override
    public boolean hasPermission(Authentication authentication, Serializable serializable,
                                 String targetUrl, Object permission) {
        return false;
    }
}

 

 

最後,再把自定義的CustomPermissionEvaluator註冊到WebSecurityConfiguration中去,也就是在WebSecurityConfiguration中加入下面的代碼:

// 注入自定義PermissionEvaluator
@Bean
public DefaultWebSecurityExpressionHandler webSecurityExpressionHandler() {
    DefaultWebSecurityExpressionHandler handler = new DefaultWebSecurityExpressionHandler();
    handler.setPermissionEvaluator(permissionEvaluator);
    return handler;
}

 

 

運行postman進行測試,注意:啟動時要在配置文件中加入下面這個配置:

spring.main.allow-bean-definition-overriding=true

從結果可以看到:

 

 

 


 

 

感謝您的大駕光臨!諮詢技術、產品、運營和管理相關問題,請關注後留言。歡迎騷擾,不勝榮幸~