提交 53ded9b71457e4302f8f7759f4adfee0d67287e7

作者 HT 黄涛
1 个父辈 4ecee3af

add controller

... ... @@ -32,6 +32,16 @@
32 32 <artifactId>mybatis-spring-boot-starter</artifactId>
33 33 <version>2.1.0</version>
34 34 </dependency>
  35 + <dependency>
  36 + <groupId>org.springframework.security.oauth</groupId>
  37 + <artifactId>spring-security-oauth2</artifactId>
  38 + <version>2.3.6.RELEASE</version>
  39 + </dependency>
  40 + <dependency>
  41 + <groupId>org.springframework.security</groupId>
  42 + <artifactId>spring-security-jwt</artifactId>
  43 + <version>1.0.10.RELEASE</version>
  44 + </dependency>
35 45
36 46 <dependency>
37 47 <groupId>org.postgresql</groupId>
... ... @@ -48,6 +58,8 @@
48 58 <artifactId>spring-boot-starter-test</artifactId>
49 59 <scope>test</scope>
50 60 </dependency>
  61 +
  62 +
51 63 </dependencies>
52 64
53 65 <build>
... ...
  1 +package com.chinadci.rdc.apidemo.config;
  2 +
  3 +import org.springframework.context.annotation.Configuration;
  4 +import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  5 +import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  6 +import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  7 +
  8 +@Configuration
  9 +@EnableResourceServer
  10 +public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  11 + @Override
  12 + public void configure(HttpSecurity http) throws Exception
  13 + {
  14 + http.authorizeRequests().antMatchers("/order/*")
  15 + .authenticated();
  16 + }
  17 +
  18 +
  19 +}
... ...
  1 +package com.chinadci.rdc.apidemo.controller;
  2 +
  3 +import org.apache.tomcat.util.http.parser.Authorization;
  4 +import org.slf4j.Logger;
  5 +import org.slf4j.LoggerFactory;
  6 +import org.springframework.security.core.Authentication;
  7 +import org.springframework.security.core.context.SecurityContextHolder;
  8 +import org.springframework.web.bind.annotation.GetMapping;
  9 +import org.springframework.web.bind.annotation.PathVariable;
  10 +import org.springframework.web.bind.annotation.RestController;
  11 +
  12 +@RestController
  13 +public class TestEndPoints {
  14 + private Logger logger = LoggerFactory.getLogger(TestEndPoints.class);
  15 +
  16 + @GetMapping("/product/{id}")
  17 + public String getProduct(@PathVariable String id)
  18 + {
  19 + Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  20 + logger.debug("authentication:{}", authentication);
  21 + return "product id:" + id;
  22 + }
  23 +
  24 + @GetMapping("/order/{id}")
  25 + public String getOrder(@PathVariable String id)
  26 + {
  27 + return "Order id:" +id;
  28 + }
  29 +}
... ...
  1 +server:
  2 + port: 8080
  3 + servlet:
  4 + context-path: /api
  5 +
  6 +spring:
  7 + datasource:
  8 + name: postgresqlDataSource
  9 + url: jdbc:postgresql://192.168.200.149:5432/gisdata
  10 + username: gisdata
  11 + password: gisdata
  12 +
  13 +security:
  14 + oauth2:
  15 + resource:
... ...
注册登录 后发表评论