From 6cb000a8e489430f3182b10ae2a081e52dedd05f Mon Sep 17 00:00:00 2001 From: MariaRoz Date: Sun, 13 Aug 2017 19:06:55 +0300 Subject: [PATCH 1/2] new commit --- src/main/java/feedback/controllers/BusinessController.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/feedback/controllers/BusinessController.java b/src/main/java/feedback/controllers/BusinessController.java index 0fd8aa3..19eb5c6 100644 --- a/src/main/java/feedback/controllers/BusinessController.java +++ b/src/main/java/feedback/controllers/BusinessController.java @@ -7,7 +7,8 @@ @RestController public class BusinessController { - @Autowired BusinessService businessService; + @Autowired + BusinessService businessService; // @RequestMapping(value = "/business/", method = RequestMethod.GET) // public ResponseEntity> listAllUsers() { @@ -20,9 +21,6 @@ public class BusinessController { // } - - - @RequestMapping("/") String hello() { return "hello world"; From ebb77868888750d563d741c9514a91f2ef372c14 Mon Sep 17 00:00:00 2001 From: MariaRoz Date: Fri, 1 Sep 2017 15:14:59 +0300 Subject: [PATCH 2/2] new commit --- pom.xml | 8 +++- src/main/java/feedback/Application.java | 5 +++ .../WebSecurityConfig.java | 9 ++-- .../controllers/BusinessController.java | 41 +++++++++++++------ .../feedback/controllers/UserController.java | 24 +++++------ src/main/java/feedback/models/Authority.java | 24 +++++++++++ src/main/java/feedback/models/Business.java | 22 ++++++---- .../repositories/BusinessRepository.java | 10 ++--- .../security/TokenAuthenticationService.java | 3 +- .../feedback/services/BusinessService.java | 8 ++-- .../services/BusinessServiceImpl.java | 38 +++++++++++++++++ .../servicesImpl/BusinessServiceImpl.java | 25 ----------- src/main/resources/application.properties | 1 - src/main/resources/create.sql | 16 +++++--- src/main/resources/static/index.html | 35 ++++++++++++++++ 15 files changed, 189 insertions(+), 80 deletions(-) rename src/main/java/feedback/{security => config}/WebSecurityConfig.java (85%) create mode 100644 src/main/java/feedback/models/Authority.java create mode 100644 src/main/java/feedback/services/BusinessServiceImpl.java delete mode 100644 src/main/java/feedback/services/servicesImpl/BusinessServiceImpl.java create mode 100644 src/main/resources/static/index.html diff --git a/pom.xml b/pom.xml index 98dbd87..58fa414 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,13 @@ org.springframework.boot spring-boot-starter-data-jpa - + + org.webjars + bootstrap + 3.3.7 + + + diff --git a/src/main/java/feedback/Application.java b/src/main/java/feedback/Application.java index a56ec88..da78bda 100644 --- a/src/main/java/feedback/Application.java +++ b/src/main/java/feedback/Application.java @@ -1,9 +1,14 @@ package feedback; import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication +@EnableAutoConfiguration +//@ComponentScan(basePackages = {"feedback.controllers","feedback.services" } ) +//@EntityScan("feedback.models") +//@EnableJpaRepositories("feedback.repositories") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); diff --git a/src/main/java/feedback/security/WebSecurityConfig.java b/src/main/java/feedback/config/WebSecurityConfig.java similarity index 85% rename from src/main/java/feedback/security/WebSecurityConfig.java rename to src/main/java/feedback/config/WebSecurityConfig.java index 447c868..e1ea493 100644 --- a/src/main/java/feedback/security/WebSecurityConfig.java +++ b/src/main/java/feedback/config/WebSecurityConfig.java @@ -1,16 +1,19 @@ -package feedback.security; +package feedback.config; +import feedback.security.JWTAuthenticationFilter; +import feedback.security.JWTLoginFilter; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 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.web.authentication.UsernamePasswordAuthenticationFilter; @Configuration -@EnableWebSecurity +@EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable().authorizeRequests() diff --git a/src/main/java/feedback/controllers/BusinessController.java b/src/main/java/feedback/controllers/BusinessController.java index 19eb5c6..34d7f1b 100644 --- a/src/main/java/feedback/controllers/BusinessController.java +++ b/src/main/java/feedback/controllers/BusinessController.java @@ -1,28 +1,45 @@ package feedback.controllers; +import feedback.models.Business; import feedback.services.BusinessService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; + @RestController public class BusinessController { @Autowired BusinessService businessService; -// @RequestMapping(value = "/business/", method = RequestMethod.GET) -// public ResponseEntity> listAllUsers() { -// List businesses = businessService.getAll() ; -// if (businesses.isEmpty()) { -// return new ResponseEntity(HttpStatus.NO_CONTENT); -// // You many decide to return HttpStatus.NOT_FOUND -// } -// return new ResponseEntity>(businesses, HttpStatus.OK); -// } + + @RequestMapping(method = GET, value = "/bus/{userId}") + public Business loadById(@PathVariable Long busiessId) { + return this.businessService.findById(busiessId); + } + + @RequestMapping(method = GET, value = "/bus/all") + public List loadAll() { + return this.businessService.findAll(); + } + + @RequestMapping("/whoami") + @PreAuthorize("hasRole('USER')") + public Business business() { + return (Business) SecurityContextHolder + .getContext() + .getAuthentication() + .getPrincipal(); - @RequestMapping("/") - String hello() { - return "hello world"; +// @RequestMapping("/") +// String hello() { +// return "hello world"; } } diff --git a/src/main/java/feedback/controllers/UserController.java b/src/main/java/feedback/controllers/UserController.java index b97491e..1934935 100644 --- a/src/main/java/feedback/controllers/UserController.java +++ b/src/main/java/feedback/controllers/UserController.java @@ -1,16 +1,12 @@ package feedback.controllers; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class UserController { - @RequestMapping("/users") - public - @ResponseBody - String getUsers() { - return "{\"users\":[{\"firstname\":\"Richard\", \"lastname\":\"Feynman\"}," + - "{\"firstname\":\"Marie\",\"lastname\":\"Curie\"}]}"; - } -} \ No newline at end of file +//@RestController +//public class UserController { +// @RequestMapping("/users") +// public +// @ResponseBody +// String getUsers() { +// return "{\"users\":[{\"firstname\":\"Richard\", \"lastname\":\"Feynman\"}," + +// "{\"firstname\":\"Marie\",\"lastname\":\"Curie\"}]}"; +// } +//} \ No newline at end of file diff --git a/src/main/java/feedback/models/Authority.java b/src/main/java/feedback/models/Authority.java new file mode 100644 index 0000000..3bf263a --- /dev/null +++ b/src/main/java/feedback/models/Authority.java @@ -0,0 +1,24 @@ +package feedback.models; + +import org.springframework.security.core.GrantedAuthority; + +import javax.persistence.*; + +@Entity +@Table(name = "authority") +public class Authority implements GrantedAuthority { + + @Id + @Column(name = "id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + Long id; + + @Column(name = "name") + String name; + + @Override + public String getAuthority() { + return name; + } + +} diff --git a/src/main/java/feedback/models/Business.java b/src/main/java/feedback/models/Business.java index a52f5c4..1d27449 100644 --- a/src/main/java/feedback/models/Business.java +++ b/src/main/java/feedback/models/Business.java @@ -1,5 +1,6 @@ package feedback.models; +import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import javax.persistence.*; @@ -15,17 +16,24 @@ public class Business { private Long id; @Column(name = "person_name") - private String name; + private String businessname; + + @JsonIgnore + @Column(name = "password") + private String password; @Column(name = "description") private String description; - private Business() { - } +// @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) +// @JoinTable(name = "user_authority", +// joinColumns = @JoinColumn(name = "business_id", referencedColumnName = "id"), +// inverseJoinColumns = @JoinColumn(name = "authority_id", referencedColumnName = "id")) +// private List authorities; + +// public void setAuthorities(List authorities) { +// this.authorities = authorities; +// } - public Business(String name, String description) { - this.name = name; - this.description = description; - } } diff --git a/src/main/java/feedback/repositories/BusinessRepository.java b/src/main/java/feedback/repositories/BusinessRepository.java index 8ab3e25..965725b 100644 --- a/src/main/java/feedback/repositories/BusinessRepository.java +++ b/src/main/java/feedback/repositories/BusinessRepository.java @@ -1,11 +1,11 @@ package feedback.repositories; -import org.springframework.data.repository.CrudRepository; import feedback.models.Business; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; -import java.util.List; - -public interface BusinessRepository extends CrudRepository { - List findAll(); +@Repository +public interface BusinessRepository extends JpaRepository { + Business findByBusinessname(String username); } diff --git a/src/main/java/feedback/security/TokenAuthenticationService.java b/src/main/java/feedback/security/TokenAuthenticationService.java index d8cd0ee..e8fe754 100644 --- a/src/main/java/feedback/security/TokenAuthenticationService.java +++ b/src/main/java/feedback/security/TokenAuthenticationService.java @@ -13,7 +13,7 @@ import static java.util.Collections.emptyList; class TokenAuthenticationService { - static final long EXPIRATIONTIME = 864_000_000; // 10 days + static final long EXPIRATIONTIME = 864_000_000; static final String SECRET = "ThisIsASecret"; static final String TOKEN_PREFIX = "Bearer"; static final String HEADER_STRING = "Authorization"; @@ -30,7 +30,6 @@ static void addAuthentication(HttpServletResponse res, String username) { static Authentication getAuthentication(HttpServletRequest request) { String token = request.getHeader(HEADER_STRING); if (token != null) { - // parse the token. String user = Jwts.parser() .setSigningKey(SECRET) .parseClaimsJws(token.replace(TOKEN_PREFIX, "")) diff --git a/src/main/java/feedback/services/BusinessService.java b/src/main/java/feedback/services/BusinessService.java index 6072e1d..e54e15b 100644 --- a/src/main/java/feedback/services/BusinessService.java +++ b/src/main/java/feedback/services/BusinessService.java @@ -2,12 +2,10 @@ import feedback.models.Business; -import java.sql.SQLException; import java.util.List; public interface BusinessService { - List getAll() throws SQLException; - - Business getBusinessById(Integer id) throws SQLException; - + Business findById(Long id); + Business findByBusinessname(String username); + List findAll(); } diff --git a/src/main/java/feedback/services/BusinessServiceImpl.java b/src/main/java/feedback/services/BusinessServiceImpl.java new file mode 100644 index 0000000..684f10e --- /dev/null +++ b/src/main/java/feedback/services/BusinessServiceImpl.java @@ -0,0 +1,38 @@ +package feedback.services; + +import feedback.models.Business; +import feedback.repositories.BusinessRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class BusinessServiceImpl implements BusinessService { + @Autowired + BusinessRepository businessRepository; + + @Override + @PreAuthorize("hasRole('USER')") + public Business findByBusinessname(String businessname) throws UsernameNotFoundException { + Business business = businessRepository.findByBusinessname(businessname); + return business; + } + + @Override + @PreAuthorize("hasRole('ADMIN')") + public Business findById(Long id) throws AccessDeniedException { + Business business = businessRepository.findOne(id); + return business; + } + + @Override + @PreAuthorize("hasRole('ADMIN')") + public List findAll() throws AccessDeniedException { + List result = businessRepository.findAll(); + return result; + } +} \ No newline at end of file diff --git a/src/main/java/feedback/services/servicesImpl/BusinessServiceImpl.java b/src/main/java/feedback/services/servicesImpl/BusinessServiceImpl.java deleted file mode 100644 index 1ac102f..0000000 --- a/src/main/java/feedback/services/servicesImpl/BusinessServiceImpl.java +++ /dev/null @@ -1,25 +0,0 @@ -package feedback.services.servicesImpl; - -import feedback.models.Business; -import feedback.repositories.BusinessRepository; -import feedback.services.BusinessService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.List; - -@Service -public class BusinessServiceImpl implements BusinessService { - @Autowired - BusinessRepository businessRepository; - - @Override - public List getAll() { - return businessRepository.findAll(); - } - - @Override - public Business getBusinessById(Integer id) { - return null; - } -} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 184d53e..543ecbb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,7 +2,6 @@ server.port=8080 server.context-path=/springboot management.security.enabled=false - spring.jpa.database=POSTGRESQL spring.datasource.platform=postgres spring.jpa.show-sql=true diff --git a/src/main/resources/create.sql b/src/main/resources/create.sql index 81ecae7..7544062 100644 --- a/src/main/resources/create.sql +++ b/src/main/resources/create.sql @@ -1,5 +1,11 @@ -CREATE TABLE business -( id SERIAL NOT NULL PRIMARY KEY, - person_name varchar(200) NOT NULL , - description varchar(200) NOT NULL -); \ No newline at end of file +-- CREATE TABLE business +-- ( id SERIAL NOT NULL PRIMARY KEY, +-- person_name varchar(200) NOT NULL , +-- description varchar(200) NOT NULL +-- ); + + +-- CREATE TABLE authority +-- ( id SERIAL NOT NULL PRIMARY KEY, +-- name varchar(200) NOT NULL , +-- ); \ No newline at end of file diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html new file mode 100644 index 0000000..35d0e0a --- /dev/null +++ b/src/main/resources/static/index.html @@ -0,0 +1,35 @@ + + + + + + springboot-starter + + + + +
+ +
+
+ + + + + + + + + + + + +