Jun 21

若依后台系统整合oauth 不指定

bkkkd , 11:15 , 开发应用 , 评论(0) , 引用(0) , 阅读(4638) , Via 本站原创 | |
最近公司准备开始一个新项目,而这次客户要求是使用java.
为了节省时间的我,选择了若依后台系统.但发现没有整合oauth
(虽然其它版本有整合,但使用分离开发成本有点高.对于我们这种没有前后端分离的小团队不划算.)

在 ruoyi-framework/pom.xml添加以下的依赖


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.3.8.RELEASE</version>
        </dependency>


ruoyi-framework/src/main/java/com/ruoyi.framework/config/AuthorizationServerConfiguration.java
这个文件主要的作用是启用认证服务,其中的关键代码以下内容.其它的都是配置Bean
[quote]
// 启动认证服务
@EnableAuthorizationServer
//用来配置令牌端点(Token Endpoint)的安全约束。
public void configure(AuthorizationServerSecurityConfigurer security);
//配置OAuth2的客户端相关信息
public void configure(AuthorizationServerEndpointsConfigurer endpoints);
//配置授权服务器端点的属性
public void configure(ClientDetailsServiceConfigurer clients)
[/code]
ruoyi-framework/src/main/java/com/ruoyi.framework/config/ResourceServerConfiguration.java
这里主要配置资源route的拦截.

// 启动资源服务
@EnableResourceServer
    public void configure(HttpSecurity http) throws Exception {
        http.requestMatchers().antMatchers("/api/**") //仅拦截资源服务相关请求
                .and().authorizeRequests().anyRequest().authenticated();
    }


另外要修改ShiroConfig.java.因为若依的后台是使用shiro进行验证.与spring-security-oauth不是同一个拦截器.所以先要在ShiroConfig.java中忽略/oauth/**和/api/**.
ruoyi-framework/src/main/java/com/ruoyi.framework/config/ShiroConfig.java

        filterChainDefinitionMap.put("/oauth/**", "anon");
        filterChainDefinitionMap.put("/api2/**", "anon");


具体的接口类


@RestController // 直接返回数据
@RequestMapping("/api/group") // 路由地址
public class Group {
    @RequestMapping("hello") // 方法的处理项
    public AjaxResult hello() {
        return AjaxResult.success("success"); // 返回成功
    }
}


后记:
org.springframework.security.oauth 将会被放弃.而会统一使用spring
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]