Skip to content

Commit d9a80dc

Browse files
committed
Documented Auth controller
1 parent a696c73 commit d9a80dc

File tree

14 files changed

+144
-32
lines changed

14 files changed

+144
-32
lines changed

build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ dependencies {
4141
compileOnly 'org.projectlombok:lombok'
4242
annotationProcessor 'org.projectlombok:lombok'
4343

44-
// swagger3
45-
compile "io.springfox:springfox-swagger2:3.0.0-SNAPSHOT"
46-
compile "io.springfox:springfox-swagger-ui:3.0.0-SNAPSHOT"
44+
// Swagger
45+
implementation ("io.springfox:springfox-swagger2:2.9.2")
46+
implementation "io.springfox:springfox-swagger-ui:2.9.2"
47+
implementation "org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE"
4748

4849
// Mapstruct
4950
implementation "org.mapstruct:mapstruct:1.3.0.Final"

src/main/java/io/github/deepanshut041/serendeepia/SerendeepiaApplication.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package io.github.deepanshut041.serendeepia;
22

3+
import io.github.deepanshut041.serendeepia.domains.Role;
4+
import io.github.deepanshut041.serendeepia.repository.RoleRepository;
5+
import org.springframework.boot.ApplicationRunner;
36
import org.springframework.boot.SpringApplication;
47
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.context.annotation.Bean;
9+
10+
import java.util.stream.Stream;
511

612
@SpringBootApplication
713
public class SerendeepiaApplication {
@@ -10,4 +16,23 @@ public static void main(String[] args) {
1016
SpringApplication.run(SerendeepiaApplication.class, args);
1117
}
1218

19+
@Bean
20+
ApplicationRunner init(RoleRepository repository) {
21+
Role[] roles = {
22+
new Role("ROLE_USER"), new Role("ROLE_ADMIN")
23+
};
24+
25+
return args -> {
26+
Stream.of(roles).forEach(
27+
role -> {
28+
if(!repository.existsByName(role.getName())) {
29+
repository.save(role);
30+
}
31+
}
32+
);
33+
34+
repository.findAll().forEach(System.out::println);
35+
};
36+
}
37+
1338
}

src/main/java/io/github/deepanshut041/serendeepia/config/SecurityConfiguration.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,10 @@ protected void configure(HttpSecurity http) throws Exception {
5555
.and()
5656
.authorizeRequests()
5757
.antMatchers(
58-
"/api/auth/**",
59-
"/api/v2/api-docs/**",
60-
"/swagger-ui.html/**",
61-
"/swagger-resources/**",
62-
"/favicon.ico", "/**/*.png", "/**/*.gif", "/**/*.svg", "/**/*.jpg", "/**/*.html", "/**/*.css", "/**/*.js"
63-
64-
)
58+
"/api/auth/**", "/**",
59+
"/v2/api-docs", "/configuration/**", "/swagger*/**",
60+
"/favicon.ico",
61+
"/**/*.png", "/**/*.gif", "/**/*.svg", "/**/*.jpg", "/**/*.html", "/**/*.css", "/**/*.js")
6562
.permitAll().anyRequest().authenticated();
6663

6764
http.addFilterBefore(tokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

src/main/java/io/github/deepanshut041/serendeepia/config/SpringFoxConfig.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import springfox.documentation.service.Contact;
1111
import springfox.documentation.spi.DocumentationType;
1212
import springfox.documentation.spring.web.plugins.Docket;
13+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
1314

1415
import java.util.Optional;
1516

1617
@Configuration
18+
@EnableSwagger2
1719
public class SpringFoxConfig {
1820

1921
@Bean
@@ -26,12 +28,12 @@ public Docket api() {
2628
.build();
2729
}
2830
private ApiInfo apiInfo() {
29-
return new ApiInfoBuilder().title("Sarte REST API")
30-
.description("Shop Management REST API")
31-
.contact(new Contact("Squrlabs", "www.Squrlabs.com", "squrlabs@gmail.com"))
31+
return new ApiInfoBuilder().title("Serendeepia REST API")
32+
.description("Serendeepia Auth REST API")
33+
.contact(new Contact("Deepanshu Tyagi", "deepanshut041.github.io", "deepanshut041@gmail.com"))
3234
.license("Apache 2.0")
3335
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
34-
.version("0.0.1")
36+
.version("1.0")
3537
.build();
3638
}
3739

src/main/java/io/github/deepanshut041/serendeepia/config/WebMvcConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.context.annotation.Configuration;
44
import org.springframework.web.servlet.config.annotation.CorsRegistry;
5+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
56
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
67

78
@Configuration
@@ -17,4 +18,12 @@ public void addCorsMappings(CorsRegistry registry) {
1718
.maxAge(MAX_AGE_SECS);
1819
}
1920

21+
@Override
22+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
23+
registry.addResourceHandler("swagger-ui.html")
24+
.addResourceLocations("classpath:/META-INF/resources/");
25+
26+
registry.addResourceHandler("/webjars/**")
27+
.addResourceLocations("classpath:/META-INF/resources/webjars/");
28+
}
2029
}

src/main/java/io/github/deepanshut041/serendeepia/controller/AuthController.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.deepanshut041.serendeepia.controller;
22

3+
import io.github.deepanshut041.serendeepia.domains.Role;
34
import io.github.deepanshut041.serendeepia.domains.User;
45
import io.github.deepanshut041.serendeepia.dto.ApiResponse;
56
import io.github.deepanshut041.serendeepia.dto.AuthResponse;
@@ -9,6 +10,8 @@
910
import io.github.deepanshut041.serendeepia.service.UserService;
1011
import io.github.deepanshut041.serendeepia.util.JwtTokenUtil;
1112
import io.github.deepanshut041.serendeepia.util.exception.BadRequestException;
13+
import io.swagger.annotations.Api;
14+
import io.swagger.annotations.ApiParam;
1215
import org.springframework.beans.factory.annotation.Autowired;
1316
import org.springframework.http.ResponseEntity;
1417
import org.springframework.security.authentication.AuthenticationManager;
@@ -22,16 +25,16 @@
2225
import org.springframework.web.bind.annotation.RestController;
2326

2427
import javax.validation.Valid;
28+
import java.util.List;
2529

2630
@RestController
2731
@RequestMapping("/api/auth")
32+
@Api(value = "Authentication Route")
2833
public class AuthController {
2934

3035
@Autowired
3136
private AuthenticationManager authenticationManager;
3237

33-
@Autowired
34-
private PasswordEncoder passwordEncoder;
3538

3639
@Autowired
3740
private JwtTokenUtil tokenUtil;
@@ -43,7 +46,9 @@ public class AuthController {
4346
private RoleService roleService;
4447

4548
@PostMapping("/login")
46-
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginDto loginDto) {
49+
public ResponseEntity<?> authenticateUser(
50+
@ApiParam(value = "LoginDto contains email and password", required = true)
51+
@Valid @RequestBody LoginDto loginDto) {
4752

4853
Authentication authentication = authenticationManager.authenticate(
4954
new UsernamePasswordAuthenticationToken(
@@ -59,22 +64,19 @@ public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginDto loginDto)
5964
}
6065

6166
@PostMapping("/register")
62-
public ResponseEntity<?> registerUser(@Valid @RequestBody RegisterDto registerDto) {
67+
public ResponseEntity<?> registerUser(
68+
@ApiParam(value = "RegisterDto contains information regarding user", required = true)
69+
@Valid @RequestBody RegisterDto registerDto) {
70+
6371
if(userService.existsByEmail(registerDto.getEmail())) {
6472
throw new BadRequestException("Email address already in use.");
6573
}
6674

67-
// Creating user's account
68-
User user = new User();
69-
user.setName(registerDto.getName());
70-
user.setEmail(registerDto.getEmail());
71-
user.setPassword(registerDto.getPassword());
72-
user.setPassword(passwordEncoder.encode(user.getPassword()));
73-
user.setRoles(roleService.findByName("ROLE_USER"));
75+
List<Role> roles = roleService.findByName("ROLE_USER");
7476

75-
userService.save(user);
77+
userService.save(registerDto, roles);
7678

77-
return ResponseEntity.ok(new ApiResponse(true, "User registered successfully@"));
79+
return ResponseEntity.ok(new ApiResponse(true, "User registered successfully"));
7880
}
7981

8082
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.github.deepanshut041.serendeepia.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
import org.springframework.web.servlet.ModelAndView;
7+
8+
@RequestMapping("")
9+
@Controller
10+
public class HomeController {
11+
12+
@RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
13+
public ModelAndView home(){
14+
return new ModelAndView("redirect:/swagger-ui.html");
15+
}
16+
}

src/main/java/io/github/deepanshut041/serendeepia/domains/Role.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ public class Role {
1818
@Column(unique = true, nullable = false)
1919
private String name;
2020

21-
@ManyToMany(mappedBy = "roles")
22-
private Collection<User> users;
21+
// @ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
22+
// private Collection<User> users;
23+
24+
public Role (String name){
25+
this.name = name;
26+
}
2327

2428
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package io.github.deepanshut041.serendeepia.dto;
22

3+
import io.swagger.annotations.ApiModel;
4+
import io.swagger.annotations.ApiModelProperty;
35
import lombok.Data;
6+
import lombok.NoArgsConstructor;
47

58
import javax.validation.constraints.Email;
69
import javax.validation.constraints.NotBlank;
710

811
@Data
12+
@NoArgsConstructor
13+
@ApiModel(value = "Login Request", description = "This Login request model")
914
public class LoginDto {
15+
1016
@NotBlank
1117
@Email
18+
@ApiModelProperty(notes = "Your email for login", required = true)
1219
private String email;
1320

1421
@NotBlank
22+
@ApiModelProperty(notes = "Your password for login", required = true)
1523
private String password;
1624
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.deepanshut041.serendeepia.dto;
2+
3+
import io.swagger.annotations.ApiModel;
4+
import io.swagger.annotations.ApiModelProperty;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
@Data
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
@ApiModel(value = "Profile Response", description = "This profile response model")
13+
public class ProfileDto {
14+
15+
@ApiModelProperty(notes = "User id, It is unique")
16+
private Long id;
17+
18+
@ApiModelProperty(notes = "User email, It is unique")
19+
private String email;
20+
21+
@ApiModelProperty(notes = "Full name user")
22+
private String name;
23+
}

0 commit comments

Comments
 (0)