get response from spring oauth2 token request as xml -
i implementing oauth2 rest server using spring 4 , spring oauth2. response can either xml or json specifeid client request header. getting issues oauth2 when ever try access token supports json (application/json), not supporting xml(application/xml).
am getting error as:
http status 406 - type status report message description resource identified request capable of generating responses characteristics not acceptable according request "accept" headers. apache tomcat/7.0.54
my security configuration follows:
@configuration @enablewebsecurity(debug = true) @componentscan(basepackages = { "org.bluez.logiczweb.config.security.handler" }) public class securityconfiguration extends websecurityconfigureradapter { private final static string applicationname = "crm"; @configuration @enableresourceserver protected static class resourceserverconfiguration extends resourceserverconfigureradapter { @autowired private headeronlyoauth2exceptionrenderer headeronlyexceptionrender; @override public void configure(resourceserversecurityconfigurer resources) { resources.resourceid(applicationname); } public void configure(httpsecurity http) throws exception { http.csrf().disable(); http.sessionmanagement().sessioncreationpolicy( sessioncreationpolicy.stateless); http.requestmatchers().and().authorizerequests() .antmatchers("/rest/accounts") .access("#oauth2.hasscope('read')").and().httpbasic() .authenticationentrypoint(clientauthenticationentrypoint()); // // .apply(new oauth2serverconfigurer()) // .tokenstore(new inmemorytokenstore()) // .resourceid(applicationname); } @bean public oauth2accessdeniedhandler oauthaccessdeniedhandler() { oauth2accessdeniedhandler accessdeniedhandler = new oauth2accessdeniedhandler(); accessdeniedhandler.setexceptionrenderer(headeronlyexceptionrender); return accessdeniedhandler; } @bean public oauth2authenticationentrypoint clientauthenticationentrypoint() { oauth2authenticationentrypoint authenticationentrypoint = new oauth2authenticationentrypoint(); authenticationentrypoint.setrealmname("sparklr2/client"); authenticationentrypoint.settypename("basic"); authenticationentrypoint .setexceptionrenderer(headeronlyexceptionrender); return authenticationentrypoint; } } @configuration @enableauthorizationserver protected static class authorizationserverconfiguration extends authorizationserverconfigureradapter { @autowired private tokenstore tokenstore; @autowired private authenticationmanager authenticationmanager; public void configure(clientdetailsserviceconfigurer clients) throws exception { final string scopes[] = "read,write,trust".split(","); final string secret = "123456"; final string[] authorizedgranttypes = { "password", "authorization_code", "refresh_token" }; final string authorities = "role_user"; clients.inmemory().withclient("android-crm") .resourceids(applicationname).scopes(scopes) .authorities(authorities) .authorizedgranttypes(authorizedgranttypes).secret(secret) .and().withclient("ios-crm").resourceids(applicationname) .scopes(scopes).authorities(authorities) .authorizedgranttypes(authorizedgranttypes).secret(secret); } @override public void configure(authorizationserverendpointsconfigurer endpoints) throws exception { endpoints.tokenstore(tokenstore).authenticationmanager( authenticationmanager); } @override public void configure(authorizationserversecurityconfigurer oauthserver) throws exception { oauthserver.realm("sparklr2/client"); } @bean public approvalstore approvalstore() throws exception { tokenapprovalstore store = new tokenapprovalstore(); store.settokenstore(tokenstore); return store; } /* * @bean public oauth2accessdeniedhandler oauthaccessdeniedhandler(){ * oauth2accessdeniedhandler accessdeniedhandler=new * oauth2accessdeniedhandler(); * * return accessdeniedhandler; } */ } @bean public authenticationmanager authenticationmanagerbean() throws exception { return super.authenticationmanagerbean(); } @bean passwordencoder passwordencoder() { return nooppasswordencoder.getinstance(); } @bean textencryptor textencryptor() { return encryptors.nooptext(); } @bean public userdetailsservice userdetailsservicebean() throws exception { return super.userdetailsservicebean(); } protected void configure(authenticationmanagerbuilder authmanagerbuilder) throws exception { authmanagerbuilder.userdetailsservice(userdetailsservicebean()) .passwordencoder(passwordencoder()).and() .inmemoryauthentication().withuser("admin").password("admin") .roles("admin", "user"); } @bean public tokenstore tokenstore() { return new inmemorytokenstore(); } }
my response json is:
{ "access_token": "27f93b60-a2ab-4ae6-90c9-81124cc7d10b", "token_type": "bearer", "refresh_token": "6bb643cf-3eda-402a-bb6d-5f3b05e56bee", "expires_in": 43199, "scope": "read" }
i want xml
my mvc configuration follows:
@enablewebmvc @configuration public class webmvcconfiguration extends webmvcconfigurationsupport { @bean public exceptionhandlerexceptionresolver exceptionhandlerexceptionresolver() { exceptionhandlerexceptionresolver exceptionresolver = new exceptionhandlerexceptionresolver(); exceptionresolver.setorder(0); exceptionresolver.setmessageconverters(messageconverters()); return exceptionresolver; } @bean public list> messageconverters() { list> messageconverters = new arraylist>(); messageconverters.add(jsonhttpmessageconverter()); // messageconverters.add(stringhttpmessageconverter()); messageconverters.add(xmlconverter()); return messageconverters; } @bean public stringhttpmessageconverter stringhttpmessageconverter() { return new stringhttpmessageconverter(); } @bean public mappingjackson2httpmessageconverter jsonhttpmessageconverter() { return new mappingjackson2httpmessageconverter(); } @bean public bytearrayhttpmessageconverter bytearrayhttpmessageconverter() { return new bytearrayhttpmessageconverter(); } @bean public requestmappinghandleradapter requestmappinghandleradapter() { requestmappinghandleradapter adapter = super .requestmappinghandleradapter(); adapter.setorder(0); adapter.getmessageconverters().addall(messageconverters()); adapter.getmessageconverters().add(bytearrayhttpmessageconverter()); return adapter; } @bean public jaxb2marshaller jaxbmarshaller() { jaxb2marshaller jaxb2marshaller = new jaxb2marshaller(); jaxb2marshaller .setpackagestoscan("org.bluez.logiczweb.core.models.vo.*"); return jaxb2marshaller; } @bean public marshallinghttpmessageconverter xmlconverter() { marshallinghttpmessageconverter converter = new marshallinghttpmessageconverter( jaxbmarshaller()); converter.setsupportedmediatypes(arrays .aslist(mediatype.application_xml)); return converter; } }
please me..
marshallinghttpmessageconverter
not smart enough convert access tokens. that's why spring oauth has jaxboauth2accesstokenmessageconverter
(and jaxboauth2exceptionmessageconverter
). need those, , need them in oauth2authenticationentrypoint
, oauth2accessdeniedhandler
used in /token
endpoint security (assuming client wants security errors rendered in xml well). current @enableauthorizationserver
support lets inject own authenticationentrypoint
, you'll need extend authorizationserversecurityconfiguration
override accessdeniedhandler
.
Comments
Post a Comment