Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlong committed Oct 21, 2016
2 parents 2808203 + bb07851 commit 0fa38f8
Show file tree
Hide file tree
Showing 27 changed files with 688 additions and 233 deletions.
45 changes: 45 additions & 0 deletions livelessons-integration/livelessons-integration-and-batch/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>livelessons</groupId>
<artifactId>livelessons-integration</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>livelessons-integration-and-batch</artifactId>
<properties>
<main.basedir>../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-integration</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.security.authentication.AuthenticationManager;
Expand All @@ -18,33 +17,39 @@
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.security.Principal;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

@EnableResourceServer
@SpringBootApplication
@EnableResourceServer
public class AuthServiceApplication {

@Bean
CommandLineRunner data(AccountRepository accountRepository) {
return args -> Stream.of("pwebb,boot", "rod,atomist", "dsyer,cloud", "jlong,spring")
.map(x -> x.split(","))
.forEach(t -> accountRepository.save(new Account(t[0], t[1], true)));
}

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

@RestController
class PrincipalRestController {

@RequestMapping("/user")
public Principal principal(Principal p) {
return p;
}
}

@Configuration
@EnableAuthorizationServer
class OAuthConfiguration extends AuthorizationServerConfigurerAdapter {
Expand All @@ -58,17 +63,17 @@ public OAuthConfiguration(AuthenticationManager authenticationManager) {

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("html5")
.secret("secret")
.scopes("read", "write", "openid")
.authorizedGrantTypes("implicit", "authorization_code", "password")
.and()

clients.inMemory()
.withClient("android")
.authorizedGrantTypes("authorization_code", "password", "implicit")
.scopes("read", "write", "openid")
.secret("secret")
.and()
.withClient("html5")
.authorizedGrantTypes("authorization_code", "password", "implicit")
.scopes("read", "write", "openid")
.authorizedGrantTypes("implicit", "authorization_code", "password");
.secret("secret");
}

@Override
Expand All @@ -77,42 +82,48 @@ public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws E
}
}

@RestController
class PrincipalRestController {
@Component
class SampleDataCLR implements CommandLineRunner {

@RequestMapping("/user")
Principal principal(Principal p) {
return p;
private final AccountRepository accountRepository;

@Autowired
public SampleDataCLR(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}

@Override
public void run(String... args) throws Exception {
Stream.of("pwebb,boot", "dsyer,cloud", "jlong,spring", "rod,atomist")
.map(x -> x.split(","))
.forEach(tuple -> accountRepository.save(new Account(tuple[0], tuple[1], true)));
}
}

@Service
class AccountUserDetailsService implements UserDetailsService {

private final AccountRepository accountRepository;

@Autowired
public AccountUserDetailsService(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}

@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
return accountRepository.findByUsername(s)
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return this.accountRepository.findByUsername(username)
.map(account -> new User(account.getUsername(),
account.getPassword(),
account.isActive(),
account.isActive(),
account.isActive(),
account.isActive(),
AuthorityUtils.createAuthorityList("ROLE_USER")
account.isActive(), account.isActive(), account.isActive(), account.isActive(),
AuthorityUtils.createAuthorityList("ROLE_ADMIN", "ROLE_USER")
))
.orElseThrow(() -> new UsernameNotFoundException("couldn't find the user!"));
.orElseThrow(() -> new UsernameNotFoundException("couldn't find " + username + "!"));
}

private final AccountRepository accountRepository;

}

interface AccountRepository extends JpaRepository<Account, Long> {

Optional<Account> findByUsername(String username);
}

Expand All @@ -122,19 +133,22 @@ class Account {
@Id
@GeneratedValue
private Long id;

private String username, password;
private boolean active;

Account() {
public Account() {// why JPA why?
}

public Account(String username, String password, boolean active) {

this.username = username;
this.password = password;
this.active = active;
}

public Long getId() {

return id;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
spring.application.name=auth-service
server.context-path=/uaa
security.sessions = if_required
server.port=9191
security.sessions=never
server.context-path=/uaa
23 changes: 23 additions & 0 deletions livelessons-security/basic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
target/
!.mvn/wrapper/maven-wrapper.jar

### STS ###
.classpath
.factorypath
.project
.settings
.springBeans

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip
Loading

0 comments on commit 0fa38f8

Please sign in to comment.