[débutant spring] Resource Server Oauth2
Bonjour,
Je suis débutant en spring et j'aimerais savoir comment monter un serveur de resource en spring.
Voilà ce que j'ai fait et bien sûr ça ne marche pas.
Nous avons un server OpenidConnect CAS. On arrive bien à s'authentifier dessus avec une appli angular. Il y a un clientId, un clientsecret, on obtient bien un token valide pour quelques heures.
D'abord mon application.yaml
Je me demande si la configuration est suffisante et/ou bonne.
Code:
1 2 3 4 5 6 7 8 9
| server:
port : 8082
address: dock-dsi-02.utbm.fr
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://casdev.utbm.fr/oidc |
Mon pom.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build> |
Ici mon App.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| @SpringBootApplication
@RestController
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled =true, jsr250Enabled = true)
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
} |
ensuite les corsfilters je n'ai pas de doutes sur le fait que ça puisse fonctionner
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| @Component
public class SimpleCORSFilter implements Filter {
private final Logger log = LoggerFactory.getLogger(SimpleCORSFilter.class);
public SimpleCORSFilter() {
log.info("SimpleCORSFilter init!!!!!!!!!!!!!!!!!!!!!");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
System.out.println(request.getAttribute("Authorization")+"?????????????????????????????????");
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "28800");
response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, remember-me, x-requested-With,, x-auth-token, x-xsrf-token");
chain.doFilter(req, res);
}
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void destroy() {
}
} |
Ensuite
Il y a le resource server configuration
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| @Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.cors().and()
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/auth/**").authenticated();
}
} |
et enfin mes webservices
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| public class MainController {
//@PreAuthorize("permitAll")
@GetMapping("/auth/coucou")
String coucou() {
return "Coucou";
}
//@Secured("BLAH")
@GetMapping("/auth")
String hello() {
return "Hello ";
}
@RequestMapping("/token")
public Map<String,String> token(HttpSession session) {
return Collections.singletonMap("token", session.getId());
}
@RequestMapping("/")
public Message home() {
return new Message("Hello World");
}
class Message {
private String id = UUID.randomUUID().toString();
private String content;
Message() {
}
public Message(String content) {
this.content = content;
}
public String getId() {
return id;
}
public String getContent() {
return content;
}
}
} |
et voici un exemple de ce que je reçoit lorsque j'essaie de me connecter.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Response 401
Pragma: no-cache
WWW-Authenticate: Bearer realm="oauth2-resource", error="invalid_token", error_description="Invalid access token: AT-15-IS20xxaDGiI6YeRCIdjms9J5bHOurdYV"
Cache-Control: no-store
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 27 Mar 2019 10:13:56 GMT
{
"error": "invalid_token",
"error_description": "Invalid access token: AT-15-IS20xxaDGiI6YeRCIdjms9J5bHOurdYV"
} |
- Lorsque j'essaie de me connecter sans token sur un webservice non protégé ça va, j'ai une réponse 200 et on passe bien dans le corsfilter car j'ai un mesage.
- Sur un webservice protégé il me jette (donc c'est normal),
=> Par contre dès que j'essaie d'incorporer mon token dans ma requête... j'ai le message d'erreur http ci dessus. Que faire ????
Sur le server on ne voit aucune trace dans ce cas, même pas l'exécution de mon corsfilter.