1

The problem that arises is using the credentials with SOAP service through the username and password. I use SpringBoot framework for OAuth Server (OAuth server + resource + client). Consuming SOAP WS is in Resource Server. My code is:

First, with JaxB create SOAP classes. build.gradle

configurations {
    jaxb
}

buildscript {
    ext {
        springBootVersion = '1.3.0.RELEASE'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    }
}

group 'dynamind'
version '0.1-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'

jar {
    baseName = rootProject.name
    version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}

// tag::wsdl[]
task genJaxb {
    ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
    ext.classesDir = "${buildDir}/classes/jaxb"
    ext.schema = "http://192.168.2.171:5555/ws/Ug.accountManagementBusinessService.services.ws:accountManagementWS?wsdl"

    outputs.dir classesDir

    doLast() {
        project.ant {
            taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask", classpath: configurations.jaxb.asPath
            mkdir(dir: sourcesDir)
            mkdir(dir: classesDir)

            xjc(destdir: sourcesDir, schema: schema, package: "accountManagement.wsdl") {
                arg(value: "-wsdl")
                arg(value: "-XautoNameResolution")
                produces(dir: sourcesDir, includes: "**/*.java")
            }

            javac(destdir: classesDir, source: 1.8, target: 1.8, debug: true,
                    debugLevel: "lines,vars,source",
                    classpath: configurations.jaxb.asPath) {
                src(path: sourcesDir)
                include(name: "**/*.java")
                include(name: "*.java")
            }

            copy(todir: classesDir) {
                fileset(dir: sourcesDir, erroronmissingdir: false) {
                    exclude(name: "**/*.java")
                }
            }
        }
    }
}
// end::wsdl[]

dependencies {
    // By excluding Tomcat from starter-web and adding it as a provided dependency, 
    // we can deploy as a WAR on a servlet container or run as a standalone application
    compile(group: 'org.springframework.boot', name: 'spring-boot-starter-web') {
        exclude(module: 'spring-boot-starter-tomcat')
        exclude(group: 'org.apache.tomcat.embed')
    }
    providedCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat')

    compile(group: 'org.springframework.boot', name: 'spring-boot-starter-security')
    compile(group: 'org.springframework.security', name: 'spring-security-jwt', version: '1.0.3.RELEASE')
    compile(group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.0.8.RELEASE') {
        exclude(module: 'jackson-mapper-asl') // We already have a more recent jackson via Spring Boot
    }

    compile(group: 'org.springframework.boot', name: 'spring-boot-starter')
    compile(group: 'org.springframework.ws', name: 'spring-ws-core')
    compile(files(genJaxb.classesDir).builtBy(genJaxb))

    jaxb "com.sun.xml.bind:jaxb-xjc:2.1.7"

}

jar {
    from genJaxb.classesDir
}

// Create the wrapper support files by running 'gradle wrapper',
task wrapper(type: Wrapper) {
    gradleVersion = '2.9'
}

task afterEclipseImport {
    dependsOn genJaxb
}

Then, create class AccountConfiguration for configurate marshaller AccountConfiguration.java

@Configuration
public class AccountConfiguration {

@Bean
public Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setContextPath("accountManagement.wsdl");
    return marshaller;
}

@Bean
@Autowired
public AccountClient accountClient(Jaxb2Marshaller marshaller) {
    AccountClient client = new AccountClient();

    client.setDefaultUri("http://192.168.2.171:5555/ws/Ug.accountManagementBusinessService.services.ws:accountManagementWS");
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);

    return client;
}
}

Create AccountClient class for use SOAP methods. AccountClient.java

public class AccountClient extends WebServiceGatewaySupport {

private static final Logger log = LoggerFactory.getLogger(AccountClient.class);

public LogInResponse getLogIn(LogIn request) {

    LogInResponse response = (LogInResponse) getWebServiceTemplate()
            .marshalSendAndReceive(
                    "http://192.168.2.171:5555/ws/Ug.accountManagementBusinessService.services.ws:accountManagementWS",
                    request);

    if (response != null) {
        log.info("Value: " + response.getLoginResponse().getStatus());
    }

    return response;
}
}

Then, in Resource server I have RESTController class UserController.java

@RestController
@PreAuthorize("hasRole('USER')")
public class UserController {

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

@RequestMapping("/uid")
public LogInResponse resourceUid() {

    String username = "test_user";
    String password = "test_password";
    String countryCode = "RS";

    LogIn request = new LogIn();
    request.setAccountUsername(username);
    request.setAccountPassword(password);
    request.setCountryCode(countryCode);

    AccountClient client = new AccountClient();

    LogInResponse response = client.getLogIn(request);

    return response;
}
}

Resource server: Application.java

@SpringBootApplication
@EnableResourceServer
public class Application extends SpringBootServletInitializer {

private static final Logger log = LoggerFactory.getLogger(Application.class);

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

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
}

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
protected static class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests().anyRequest().authenticated()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
        ;
    }
}
}

When I start:

http://localhost:8080/client/uid?code=9vy9pS&state=ngPIv3#access_token=eyJhbGciOiJSUzI1NiJ9.eyJleHAiOjE0NjU0MjQyMDgsInVzZXJfbmFtZSI6InVzZXIiLCJhdXRob3JpdGllcyI6WyJST0xFX1VTRVIiXSwianRpIjoiYzY2NmFhNzctMWE3My00MDczLTlmMzEtYjU1NjYwMWE5MTg0IiwiY2xpZW50X2lkIjoiZm94cGxheS1ycyIsInNjb3BlIjpbInJlYWQiXX0.fdfN3VEOM0ZYu8bDCbcxoWDSWJcj1IDotPwdM-Ah-E-ceJvslFhUU3LCBwtX479-kgZVbbTI76ccvDvLBVX4U7iiOnhjxKiautrlQiJZ8N4ZCA1hYn9xtNbb0AU0YT2UbKhd_NZHj15N6V1jkzSMn4uoqDO7pjg2lrYbmKnuilM&token_type=bearer&expires_in=43199&scope=read&jti=c666aa77-1a73-4073-9f31-b556601a9184

I got en errror:

Servlet.service() for servlet [dispatcherServlet] in context with path [/api] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No marshaller registered. Check configuration of WebServiceTemplate.] with root cause

Krstic
  • 11
  • 1
  • 2
  • Possible duplicate of [java.lang.IllegalStateException: No unmarshaller registered. Check configuration of WebServiceTemplate](http://stackoverflow.com/questions/34765744/java-lang-illegalstateexception-no-unmarshaller-registered-check-configuration) – seenukarthi Jun 08 '16 at 10:25
  • 3
    Don't create an instance of `AccountClient` use the spring configured one. – M. Deinum Jun 08 '16 at 11:10
  • 2
    @M.Deinum is correct. You need to either Autowire it to your controller or assign it via the application context's getBean method, in order to get access to your bean. Otherwise, you'll need to manually set the configuration work you did in the bean, after the line you instantiated it with. M.Deinum should create the official answer for this. – Rick Oct 10 '16 at 15:58

0 Answers0