diff --git a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/RestApplication.java b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/RestApplication.java index 5ec2e87ab6063377c359fb1db8caf16a8ada9e52..396bf36dfa1cf1cb79a21361b08e32a8db120bda 100644 --- a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/RestApplication.java +++ b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/RestApplication.java @@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RestApplication { - public static void main(String[] args) { - SpringApplication.run(RestApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(RestApplication.class, args); + } } diff --git a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/config/RestConfiguration.java b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/config/RestConfiguration.java index b5f94abcb5fbe5836a66ef7364fa346c7be9b296..4e6bf1dcac33f7463e204eccbed486456686c959 100644 --- a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/config/RestConfiguration.java +++ b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/config/RestConfiguration.java @@ -1,26 +1,11 @@ package cz.muni.fi.pa165.icehockeymanager.rest.config; import cz.muni.fi.pa165.icehockeymanager.config.ApplicationConfig; -import cz.muni.fi.pa165.icehockeymanager.config.PersistanceApplicationConfig; -import cz.muni.fi.pa165.icehockeymanager.rest.security.JWTAuthenticationFilter; -import cz.muni.fi.pa165.icehockeymanager.rest.security.JWTAuthorizationFilter; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; -import org.springframework.http.HttpMethod; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.transaction.annotation.EnableTransactionManagement; -import org.springframework.web.cors.CorsConfiguration; -import org.springframework.web.cors.CorsConfigurationSource; -import org.springframework.web.cors.UrlBasedCorsConfigurationSource; - -import static cz.muni.fi.pa165.icehockeymanager.rest.security.SecurityConstants.SIGN_UP_URL; @Configuration @EnableTransactionManagement diff --git a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/controller/IceHockeyManagerApi.java b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/controller/IceHockeyManagerApi.java index 913c759e65c6ae3dbd5a78f11d8f8d83688a4d79..603700569f2368aa7acb50a8192bb0dac41d48dd 100644 --- a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/controller/IceHockeyManagerApi.java +++ b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/controller/IceHockeyManagerApi.java @@ -1,11 +1,19 @@ package cz.muni.fi.pa165.icehockeymanager.rest.controller; -import cz.muni.fi.pa165.icehockeymanager.dto.*; +import cz.muni.fi.pa165.icehockeymanager.dto.PlayerCreateDto; +import cz.muni.fi.pa165.icehockeymanager.dto.PlayerTransferDto; +import cz.muni.fi.pa165.icehockeymanager.dto.TeamDto; import cz.muni.fi.pa165.icehockeymanager.facades.TeamManagerFacade; import cz.muni.fi.pa165.icehockeymanager.facades.UserAuthFacade; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; diff --git a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/controller/LeagueManagerController.java b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/controller/LeagueManagerController.java index 5d8660404a26fae4f434514922f1df1fbdf3e81f..1ad3edea418bb8e3dd6fdcdd9117df7398d86a74 100644 --- a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/controller/LeagueManagerController.java +++ b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/controller/LeagueManagerController.java @@ -4,7 +4,6 @@ import cz.muni.fi.pa165.icehockeymanager.dto.GameCreateDto; import cz.muni.fi.pa165.icehockeymanager.dto.GameUpdateScoreDto; import cz.muni.fi.pa165.icehockeymanager.dto.GameWinnerDto; import cz.muni.fi.pa165.icehockeymanager.dto.TeamCreateDto; -import cz.muni.fi.pa165.icehockeymanager.dto.TeamDto; import cz.muni.fi.pa165.icehockeymanager.facades.LeagueManagerFacade; import cz.muni.fi.pa165.icehockeymanager.facades.TeamManagerFacade; import cz.muni.fi.pa165.icehockeymanager.facades.UserAuthFacade; @@ -12,9 +11,7 @@ import cz.muni.fi.pa165.icehockeymanager.facades.UserFacade; import cz.muni.fi.pa165.icehockeymanager.rest.security.JWTAuthenticationFilter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; @@ -28,12 +25,11 @@ import javax.validation.Valid; @RequestMapping("/pa165/api/manage/league") public class LeagueManagerController { - Logger logger = LoggerFactory.getLogger(JWTAuthenticationFilter.class); - private final UserFacade userFacade; private final LeagueManagerFacade leagueManagerFacade; private final TeamManagerFacade teamManagerFacade; private final UserAuthFacade userAuthFacade; + Logger logger = LoggerFactory.getLogger(JWTAuthenticationFilter.class); public LeagueManagerController(UserFacade userFacade, LeagueManagerFacade leagueManagerFacade, TeamManagerFacade teamManagerFacade, UserAuthFacade userAuthFacade) { this.userFacade = userFacade; diff --git a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/controller/PublicController.java b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/controller/PublicController.java index 684af2ebed539fc8d49e20ac5a5f5ee5d9b8a772..4528ee4be1b69f361e7bd419147964956bed7242 100644 --- a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/controller/PublicController.java +++ b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/controller/PublicController.java @@ -3,9 +3,7 @@ package cz.muni.fi.pa165.icehockeymanager.rest.controller; import cz.muni.fi.pa165.icehockeymanager.dto.GameDto; import cz.muni.fi.pa165.icehockeymanager.dto.PlayerDto; import cz.muni.fi.pa165.icehockeymanager.dto.TeamDto; -import cz.muni.fi.pa165.icehockeymanager.facades.LeagueManagerFacade; import cz.muni.fi.pa165.icehockeymanager.facades.TeamManagerFacade; -import cz.muni.fi.pa165.icehockeymanager.facades.UserAuthFacade; import cz.muni.fi.pa165.icehockeymanager.facades.UserFacade; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; diff --git a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/demo/DataLoader.java b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/demo/DataLoader.java index 5f8b39ebba26ddc605d2fb62a71035c96a301474..902d7305886a76e56f0a3f4d9c8e3ad739f7d6e8 100644 --- a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/demo/DataLoader.java +++ b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/demo/DataLoader.java @@ -21,6 +21,12 @@ public class DataLoader implements ApplicationRunner { this.userFacade = userFacade; } + private static TeamCreateDto createTeam(String name) { + var team = new TeamCreateDto(); + team.setName(name); + return team; + } + @Override public void run(ApplicationArguments args) throws Exception { leagueManager.createNewTeam(createTeam("Toronto Maple Leafs")); @@ -28,10 +34,4 @@ public class DataLoader implements ApplicationRunner { var team = teams.stream().findFirst().orElseThrow(); userAuth.createTeamManager("team_manager", "1234", team.getId().intValue()); } - - private static TeamCreateDto createTeam(String name) { - var team = new TeamCreateDto(); - team.setName(name); - return team; - } } diff --git a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/security/JWTAuthenticationFilter.java b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/security/JWTAuthenticationFilter.java index 29ee549b93807395625bf8c0613e15f325cecf12..5b074383c07f8c73d4cefaff1f55f8a075dccc86 100644 --- a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/security/JWTAuthenticationFilter.java +++ b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/security/JWTAuthenticationFilter.java @@ -12,14 +12,12 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.authority.SimpleGrantedAuthority; -import org.springframework.security.core.userdetails.User; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import javax.servlet.FilterChain; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -29,9 +27,8 @@ import static cz.muni.fi.pa165.icehockeymanager.rest.security.SecurityConstants. public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter { - Logger logger = LoggerFactory.getLogger(JWTAuthenticationFilter.class); - private final UserAuthFacade userAuthFacade; + Logger logger = LoggerFactory.getLogger(JWTAuthenticationFilter.class); public JWTAuthenticationFilter(UserAuthFacade userAuthFacade) { this.userAuthFacade = userAuthFacade; @@ -47,15 +44,15 @@ public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilte .readValue(req.getInputStream(), UserAuthRequestDto.class); logger.info(String.format("Log in attempt by %s", creds.getUsername())); var user = userAuthFacade.authenticateUser( - creds.getUsername(), - creds.getPassword() + creds.getUsername(), + creds.getPassword() ).orElseThrow(() -> new BadCredentialsException(creds.getUsername())); return new UsernamePasswordAuthenticationToken( creds.getUsername(), creds.getPassword(), List.of( new SimpleGrantedAuthority(user.getRole().toString()) - ) + ) ); } catch (IOException e) { throw new RuntimeException(e); @@ -73,7 +70,7 @@ public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilte .withExpiresAt(new Date(System.currentTimeMillis() + EXPIRATION_TIME)) .sign(Algorithm.HMAC512(SECRET.getBytes())); - String body = ((String) auth.getPrincipal()) + " " + token; + String body = auth.getPrincipal() + " " + token; res.getWriter().write(body); res.getWriter().flush(); diff --git a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/security/JWTAuthorizationFilter.java b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/security/JWTAuthorizationFilter.java index 9f83ca1b48784dc89eff4fb9ab5dcd919ba01ea8..059d8c6c229b1b9fc0b3ebff7aac04d3a736cfcb 100644 --- a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/security/JWTAuthorizationFilter.java +++ b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/security/JWTAuthorizationFilter.java @@ -16,7 +16,6 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.util.ArrayList; import java.util.List; import static cz.muni.fi.pa165.icehockeymanager.rest.security.SecurityConstants.HEADER_STRING; @@ -25,9 +24,8 @@ import static cz.muni.fi.pa165.icehockeymanager.rest.security.SecurityConstants. public class JWTAuthorizationFilter extends BasicAuthenticationFilter { - Logger logger = LoggerFactory.getLogger(JWTAuthorizationFilter.class); - private final UserAuthFacade userAuthFacade; + Logger logger = LoggerFactory.getLogger(JWTAuthorizationFilter.class); public JWTAuthorizationFilter(AuthenticationManager authManager, UserAuthFacade userAuthFacade) { super(authManager); diff --git a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/security/WebSecurity.java b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/security/WebSecurity.java index ce01645309d6e3d309b5bc9ea7aa8e9e7a448a13..62318bfd482bfe445fada6e28c1d9ce552a50f4b 100644 --- a/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/security/WebSecurity.java +++ b/rest/src/main/java/cz/muni/fi/pa165/icehockeymanager/rest/security/WebSecurity.java @@ -34,7 +34,7 @@ public class WebSecurity extends WebSecurityConfigurerAdapter { .antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll() .antMatchers(HttpMethod.GET, "/pa165/api/public/**").permitAll() .antMatchers("/pa165/api/manage/league/**").hasRole(Roles.LEAGUE_MANAGER.toString()) - .antMatchers( "/pa165/api/manage/team/**").hasRole(Roles.TEAM_MANAGER.toString()) + .antMatchers("/pa165/api/manage/team/**").hasRole(Roles.TEAM_MANAGER.toString()) .anyRequest().authenticated() .and() .addFilter(new JWTAuthenticationFilter(userAuthFacade)) diff --git a/rest/src/test/java/cz/muni/fi/pa165/icehockeymanager/rest/RestApplicationTests.java b/rest/src/test/java/cz/muni/fi/pa165/icehockeymanager/rest/RestApplicationTests.java index c54257ccac74f2a3362874163d8bbd54f4a4d252..5b2cb6a243f7068da40346ae9de9cbd6716659a1 100644 --- a/rest/src/test/java/cz/muni/fi/pa165/icehockeymanager/rest/RestApplicationTests.java +++ b/rest/src/test/java/cz/muni/fi/pa165/icehockeymanager/rest/RestApplicationTests.java @@ -6,8 +6,8 @@ import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class RestApplicationTests { - @Test - void contextLoads() { - } + @Test + void contextLoads() { + } }