Work on Swagger integration

This commit is contained in:
Claudio Maggioni 2020-03-01 12:15:43 +01:00
parent 6a244b27f1
commit 6be9c80c6e
8 changed files with 107 additions and 13 deletions

View file

@ -10,6 +10,7 @@ sourceCompatibility = "11"
repositories { repositories {
mavenCentral() mavenCentral()
jcenter()
} }
dependencies { dependencies {
@ -20,6 +21,8 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt:0.9.1' implementation 'io.jsonwebtoken:jjwt:0.9.1'
implementation 'org.springframework.security:spring-security-web' implementation 'org.springframework.security:spring-security-web'
implementation 'org.postgresql:postgresql' implementation 'org.postgresql:postgresql'
compile "io.springfox:springfox-swagger2:2.9.2"
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
implementation('org.springframework.boot:spring-boot-starter-web') { implementation('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-json' exclude group: 'org.springframework.boot', module: 'spring-boot-starter-json'

View file

@ -0,0 +1,36 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.config;
import com.google.gson.*;
import java.lang.reflect.Type;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import springfox.documentation.spring.web.json.Json;
/**
* Spring configuration in order to register the GSON type adapter needed to avoid serializing twice
* Springfox Swagger JSON output (see: https://stackoverflow.com/a/30220562)
*/
@Configuration
public class GsonConfig {
@Bean
public GsonHttpMessageConverter gsonHttpMessageConverter() {
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
converter.setGson(gson());
return converter;
}
private Gson gson() {
final GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter());
return builder.create();
}
}
/** GSON type adapter needed to avoid serializing twice Springfox Swagger JSON output */
class SpringfoxJsonToGsonAdapter implements JsonSerializer<Json> {
@Override
public JsonElement serialize(Json json, Type type, JsonSerializationContext context) {
return JsonParser.parseString(json.value());
}
}

View file

@ -0,0 +1,25 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.config;
import ch.usi.inf.sa4.sanmarinoes.smarthut.controller.AuthenticationController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@ComponentScan(basePackageClasses = {AuthenticationController.class})
public class SpringFoxConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}

View file

@ -50,13 +50,17 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.disable() .disable()
// dont authenticate this particular request // dont authenticate this particular request
.authorizeRequests() .authorizeRequests()
.antMatchers("/auth/login") .antMatchers(
"/auth/login",
"/auth/register",
"/swagger-ui.html",
"/v2/api-docs",
"/webjars/**",
"/swagger-resources/**",
"/csrf")
.permitAll() .permitAll()
.antMatchers("/auth/register")
.permitAll()
.
// all other requests need to be authenticated // all other requests need to be authenticated
anyRequest() .anyRequest()
.authenticated() .authenticated()
.and() .and()
. .

View file

@ -7,7 +7,6 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserUpdateRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal; import java.security.Principal;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException; import org.springframework.security.authentication.DisabledException;
@ -21,18 +20,31 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/auth") @RequestMapping("/auth")
public class AuthenticationController { public class AuthenticationController {
@Autowired private AuthenticationManager authenticationManager; private final AuthenticationManager authenticationManager;
@Autowired private UserRepository userRepository; private final UserRepository userRepository;
@Autowired private JWTTokenUtil jwtTokenUtil; private final JWTTokenUtil jwtTokenUtil;
@Autowired private JWTUserDetailsService userDetailsService; private final JWTUserDetailsService userDetailsService;
@Autowired private UserRepository users; private final UserRepository users;
private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
public AuthenticationController(
AuthenticationManager authenticationManager,
UserRepository userRepository,
JWTTokenUtil jwtTokenUtil,
JWTUserDetailsService userDetailsService,
UserRepository users) {
this.authenticationManager = authenticationManager;
this.userRepository = userRepository;
this.jwtTokenUtil = jwtTokenUtil;
this.userDetailsService = userDetailsService;
this.users = users;
}
@PostMapping("/login") @PostMapping("/login")
public JWTResponse login(@RequestBody JWTRequest authenticationRequest) throws Exception { public JWTResponse login(@RequestBody JWTRequest authenticationRequest) throws Exception {
authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword()); authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());

View file

@ -1,6 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import io.swagger.annotations.ApiModelProperty;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -22,11 +23,13 @@ public abstract class Device {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false) @Column(name = "id", updatable = false, nullable = false)
@ApiModelProperty(hidden = true)
private long id; private long id;
/** The room this device belongs in */ /** The room this device belongs in */
@ManyToOne @ManyToOne
@JoinColumn(name = "room_id", nullable = false, updatable = false, insertable = false) @JoinColumn(name = "room_id", nullable = false, updatable = false, insertable = false)
@ApiModelProperty(hidden = true)
private Room room; private Room room;
/** /**
@ -46,13 +49,17 @@ public abstract class Device {
* The name for the category of this particular device (e.g 'dimmer'). Not stored in the * The name for the category of this particular device (e.g 'dimmer'). Not stored in the
* database but set thanks to constructors * database but set thanks to constructors
*/ */
@Transient private final String kind; @ApiModelProperty(hidden = true)
@Transient
private final String kind;
/** /**
* The way this device behaves in the automation flow. Not stored in the database but set thanks * The way this device behaves in the automation flow. Not stored in the database but set thanks
* to constructors * to constructors
*/ */
@Transient private final FlowType flowType; @ApiModelProperty(hidden = true)
@Transient
private final FlowType flowType;
public long getId() { public long getId() {
return id; return id;

View file

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import io.swagger.annotations.ApiModelProperty;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -11,6 +12,7 @@ public class Room {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false) @Column(name = "id", updatable = false, nullable = false)
@ApiModelProperty(hidden = true)
private Long id; private Long id;
/** /**
@ -41,12 +43,14 @@ public class Room {
private String name; private String name;
/** Collection of devices present in this room */ /** Collection of devices present in this room */
@ApiModelProperty(hidden = true)
@OneToMany(mappedBy = "room") @OneToMany(mappedBy = "room")
private Set<Device> devices; private Set<Device> devices;
/** User that owns the house this room is in */ /** User that owns the house this room is in */
@ManyToOne @ManyToOne
@JoinColumn(name = "user_id", nullable = false, updatable = false, insertable = false) @JoinColumn(name = "user_id", nullable = false, updatable = false, insertable = false)
@ApiModelProperty(hidden = true)
private User user; private User user;
public Long getId() { public Long getId() {

View file

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import io.swagger.annotations.ApiModelProperty;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.*; import javax.validation.constraints.*;
@ -11,6 +12,7 @@ public class User {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false) @Column(name = "id", updatable = false, nullable = false)
@ApiModelProperty(hidden = true)
private Long id; private Long id;
/** The full name of the user */ /** The full name of the user */
@ -49,6 +51,7 @@ public class User {
/** All rooms in the user's house */ /** All rooms in the user's house */
@OneToMany(mappedBy = "user") @OneToMany(mappedBy = "user")
@ApiModelProperty(hidden = true)
private Set<Room> rooms; private Set<Room> rooms;
public Long getId() { public Long getId() {