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:
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