Skip to content

Commit

Permalink
52 finished security, starting boot
Browse files Browse the repository at this point in the history
  • Loading branch information
iuliana committed Sep 29, 2018
1 parent 49325d8 commit 9399a9d
Show file tree
Hide file tree
Showing 56 changed files with 1,334 additions and 297 deletions.
5 changes: 2 additions & 3 deletions 09-ps-data-jpa/09-ps-data-jpa.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ dependencies {
compile project(':00-ps-core')
compile misc.joda, spring.jdbc, spring.contextSupport, misc.h2, misc.commons, misc.hikari,
hibernate.ehcache, hibernate.em, hibernate.core, hibernate.validator,
spring.aop, spring.orm, spring.data, misc.aspectjweaver, misc.aspectjrt, misc.cglib
spring.aop, spring.orm, spring.data, misc.aspectjweaver, misc.aspectjrt, misc.cglib,spring.securityConfig

testCompile tests.junit, tests.easymock, tests.jmock, tests.mockito, spring.test, tests.hamcrestCore,
tests.hamcrestLib
testCompile tests.junit
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import com.ps.repos.UserRepo;
import com.ps.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.security.RolesAllowed;
import java.util.List;

import static com.ps.util.RecordBuilder.buildUser;
Expand All @@ -29,6 +31,8 @@ public UserServiceImpl(UserRepo userRepo) {
}

@Override
//@Secured("ROLE_ADMIN")
@RolesAllowed("ROLE_ADMIN")
public User findById(Long id) {
return userRepo.findOne(id);
}
Expand Down
72 changes: 0 additions & 72 deletions 09-ps-data-jpa/src/test/java/com/ps/config/db/TestDataConfig.java

This file was deleted.

74 changes: 0 additions & 74 deletions 09-ps-data-jpa/src/test/java/com/ps/repo/TestUserRepo.java

This file was deleted.

31 changes: 0 additions & 31 deletions 09-ps-data-jpa/src/test/java/com/ps/services/UserServiceTest.java

This file was deleted.

5 changes: 0 additions & 5 deletions 09-ps-data-jpa/src/test/resources/db/db.properties

This file was deleted.

27 changes: 27 additions & 0 deletions 10-ps-mvc-boot/10-ps-mvc-boot.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath boot.springBootPlugin
}
}

apply plugin: 'spring-boot'

jar {
baseName = 'mvc-boot'
version = '1.0-SNAPSHOT'
}

dependencies {

compile(boot.starterWeb) {
exclude module: "spring-boot-starter-tomcat"
}
//compile project(':09-ps-data-jpa')
compile boot.starterJetty, boot.actuator, boot.yaml

testCompile tests.junit
}

3 changes: 2 additions & 1 deletion 10-ps-mvc-boot/src/main/java/com/ps/init/AppSettings.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.book.init;
package com.ps.init;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import javax.validation.constraints.NotNull;

Expand Down
13 changes: 13 additions & 0 deletions 10-ps-mvc-boot/src/main/java/com/ps/init/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package com.ps.init;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;

/**
* Created by iuliana.cosmina on 8/15/16.
*/
@SpringBootApplication
@ComponentScan(basePackages = {"com.ps.init", "com.ps.web"})
@EnableConfigurationProperties(AppSettings.class)
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
2 changes: 1 addition & 1 deletion 10-ps-mvc-boot/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
/ | \ |_> > | \/\ ___/ \___ \ \___ \
\____|__ / __/|__| \___ >____ >____ >
========\/|__|===============\/=====\/=====\/
:: Spring Boot :: (v.1.2.6.RELEASE)
:: Spring Boot :: (v.1.4.1.RELEASE)
2 changes: 1 addition & 1 deletion 11-ps-security-practice/11-ps-security-practice.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test {

gretty {
port = 8080
contextPath = '/mvc-basic'
contextPath = '/mvc-security'
}

war {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CsrfTokenRepository;
Expand All @@ -15,17 +16,20 @@
* Created by iuliana.cosmina on 8/16/16.
*/
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
// TODO 49. Enable support for Spring Security
// TODO 50. Enable support for securing methods using JSR 250 annotations
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**","/images/**","/styles/**");
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) {
try {
auth.inMemoryAuthentication().withUser("john").password("doe").roles("USER")
.and()
.withUser("jane").password("doe").roles("USER,ADMIN")
.and().withUser("admin").password("admin").roles("ADMIN");
// TODO 51. Configure users john, jane and admin as described in home.jsp
auth.inMemoryAuthentication();
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -35,8 +39,7 @@ public void configureGlobal(AuthenticationManagerBuilder auth) {
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**","/images/**","/styles/**").permitAll()
.antMatchers("/users/show").hasRole("ADMIN")
//TODO 52. All URL matching /users/show/** must be available only to users with role ADMIN
.antMatchers("/**").hasAnyRole("ADMIN","USER")
.anyRequest()
.authenticated()
Expand Down Expand Up @@ -66,4 +69,4 @@ public CsrfTokenRepository repo() {
return repo;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public void configureDefaultServletHandling(DefaultServletHandlerConfigurer conf
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
registry.addViewController("/home").setViewName("home");
registry.addViewController("/auth").setViewName("auth");
registry.addViewController("/cancel").setViewName("cancel");
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.ps.init;

import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;

/**
* Created by iuliana.cosmina on 9/14/15.
*/
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
//TODO 48. Modify this class to register the DelegatingFilterProxy bean
public class SecurityWebApplicationInitializer {
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class WebInitializer extends AbstractAnnotationConfigDispatcherServletIni
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{
SecurityConfig.class
// TODO 53. Set the security context as root context
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public String list(Model model) {
/**
* Handles requests to show detail about one user.
*/
@RequestMapping(value = "/{id:[\\d]*}", method = RequestMethod.GET)
@RequestMapping(value = "/show/{id:[\\d]*}", method = RequestMethod.GET)
public String show(@PathVariable Long id, Model model) throws NotFoundException {
User user = userService.findById(id);
if (user == null) {
Expand Down
Loading

0 comments on commit 9399a9d

Please sign in to comment.