分享

spring-boot(十七)集成oauth2[resource]

 关平藏书 2018-01-12

上一篇文章介绍了oauth2-server端的鉴权认证部分,想了下还是将三个组件分开来讲述下,因为实际场景大概都是server,resource,client分开部署的。

这篇文章介绍下oauth2-resource端的使用。(本案例使用远程token鉴权的方式,数据库的鉴权方式我个人感觉并不是很好,此文不做阐述)

资源服务器配置项目搭建:

1.构建一个简单的maven项目

2.在项目中增加spring-boot和security及oauth的依赖支持

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>cn.majingjing.tm.oauth2</groupId>
  6. <artifactId>tm-oauth-resource</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <parent>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-parent</artifactId>
  11. <version>1.3.7.RELEASE</version>
  12. </parent>
  13. <dependencyManagement>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.cloud</groupId>
  17. <artifactId>spring-cloud-dependencies</artifactId>
  18. <version>Brixton.SR5</version>
  19. <type>pom</type>
  20. <scope>import</scope>
  21. </dependency>
  22. </dependencies>
  23. </dependencyManagement>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-security</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-oauth2</artifactId>
  32. </dependency>
  33. </dependencies>
  34. <build>
  35. <plugins>
  36. <plugin>
  37. <groupId>org.apache.maven.plugins</groupId>
  38. <artifactId>maven-compiler-plugin</artifactId>
  39. <configuration>
  40. <source>1.8</source>
  41. <target>1.8</target>
  42. </configuration>
  43. </plugin>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

3.配置服务参数及鉴权服务地址

  1. server.port=8081
  2. security.basic.enabled=false
  3. security.oauth2.resource.id=tm-oauth-resource
  4. security.oauth2.resource.token-info-uri=http://localhost:8080/oauth/check_token
  5. logging.level.root=debug

4.在主启动类上启用资源服务注解

  1. @SpringBootApplication
  2. @EnableResourceServer
  3. public class ResourceApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ResourceApplication.class, args);
  6. }
  7. }

5.编写对外提供的资源服务

  1. @RestController
  2. public class TmResourceController {
  3. private static final Logger log = LoggerFactory.getLogger(TmResourceController.class);
  4. @RequestMapping("/api/test1")
  5. public Object test1() {
  6. log.info("访问test---1---接口");
  7. Map<String, Object> m = new HashMap<>();
  8. m.put("method", "test1");
  9. m.put("time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  10. return m;
  11. }
  12. @RequestMapping("/api/user")
  13. public Object user() {
  14. log.info("访问user---user---接口");
  15. Map<String, Object> m = new HashMap<>();
  16. m.put("method", "user");
  17. m.put("name", "皇太极");
  18. m.put("age", 18);
  19. m.put("sex", "男");
  20. m.put("time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  21. return m;
  22. }
  23. }

6.添加资源的请求认证,及认证方式

  1. @Configuration
  2. public class TmResourceServerConfig extends ResourceServerConfigurerAdapter {
  3. @Autowired
  4. private ResourceServerProperties props;
  5. @Override
  6. public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
  7. resources.resourceId(props.getResourceId());
  8. }
  9. @Override
  10. public void configure(HttpSecurity http) throws Exception {
  11. http.authorizeRequests().antMatchers(HttpMethod.GET, "/api/user").authenticated();
  12. }
  13. @Bean
  14. public RemoteTokenServices remoteTokenServices() {
  15. RemoteTokenServices remoteTokenServices = new RemoteTokenServices();
  16. remoteTokenServices.setCheckTokenEndpointUrl(props.getTokenInfoUri());
  17. return remoteTokenServices;
  18. }
  19. }

7.自定义security的配置(可自行扩展,此处省略)

  1. @Configuration
  2. public class TmWebSecurityConfig extends WebSecurityConfigurerAdapter {
  3. @Override
  4. protected void configure(HttpSecurity http) throws Exception {
  5. super.configure(http);
  6. }
  7. @Override
  8. public void configure(WebSecurity web) throws Exception {
  9. web.ignoring().antMatchers("/favor.ico");
  10. }
  11. }

8.启动服务,浏览器分别访问

http://localhost:8081/api/test1

http://localhost:8081/api/user

r-1.png

r-2.png

可以看到/api/user 服务已经提示未认证需要授权访问

到此oauth2-resource端服务已经搭建完成。后续会加入oauth2-client来对这个api进行验证

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多