Integrate single sign on using spring security oauth -
i working on application guarding few rest apis spring security oauth2.the authentication works fine.now want implement single sign on feature each account.that once user login using credential 1 device not possible login same use user other device.that @ time 1 login allowed user.if wants login in device should logout login device.how in spring security oauth.below codes.
spring-security.xml :
<?xml version="1.0" encoding="utf-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:oauth="http://www.springframework.org/schema/security/oauth2" xmlns:context="http://www.springframework.org/schema/context" xmlns:sec="http://www.springframework.org/schema/security" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemalocation="http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2-2.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd "> <http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="authenticationmanager" xmlns="http://www.springframework.org/schema/security" > <intercept-url pattern="/oauth/token" access="is_authenticated_fully" method="post" /> <anonymous enabled="false" /> <http-basic entry-point-ref="clientauthenticationentrypoint" /> <custom-filter ref="clientcredentialstokenendpointfilter" before="basic_auth_filter" /> <access-denied-handler ref="oauthaccessdeniedhandler" /> </http> <http pattern="/protected/**" create-session="never" entry-point-ref="oauthauthenticationentrypoint" xmlns="http://www.springframework.org/schema/security"> <anonymous enabled="false" /> <intercept-url pattern="/protected/**" method="get" access="role_app" /> <!-- <intercept-url pattern="/resources/**" access="is_authenticated_fully" /> --> <custom-filter ref="resourceserverfilter" before="pre_auth_filter" /> <access-denied-handler ref="oauthaccessdeniedhandler" /> </http> <http pattern="/logout" create-session="never" entry-point-ref="oauthauthenticationentrypoint" xmlns="http://www.springframework.org/schema/security"> <anonymous enabled="false" /> <intercept-url pattern="/logout" method="get" /> <sec:logout invalidate-session="true" logout-url="/logout" success-handler-ref="logoutsuccesshandler" /> <custom-filter ref="resourceserverfilter" before="pre_auth_filter" /> <access-denied-handler ref="oauthaccessdeniedhandler" /> </http> <bean id="logoutsuccesshandler" class="com.example.myproject.security.logoutimpl" > <property name="tokenstore" ref="tokenstore"></property> </bean> <bean id="oauthauthenticationentrypoint" class="org.springframework.security.oauth2.provider.error.oauth2authenticationentrypoint"> <property name="exceptiontranslator" ref="myexceptiontranslator"></property> </bean> <bean id="myexceptiontranslator" class="org.springframework.security.oauth2.provider.error.defaultwebresponseexceptiontranslator"> </bean> <bean id="clientauthenticationentrypoint" class="org.springframework.security.oauth2.provider.error.oauth2authenticationentrypoint"> <property name="realmname" value="springsec/client" /> <property name="typename" value="basic" /> </bean> <bean id="oauthaccessdeniedhandler" class="org.springframework.security.oauth2.provider.error.oauth2accessdeniedhandler"> </bean> <bean id="clientcredentialstokenendpointfilter" class="org.springframework.security.oauth2.provider.client.clientcredentialstokenendpointfilter"> <property name="authenticationmanager" ref="authenticationmanager" /> </bean> <authentication-manager alias="authenticationmanager" xmlns="http://www.springframework.org/schema/security"> <authentication-provider user-service-ref="clientdetailsuserservice" /> </authentication-manager> <bean id="clientdetailsuserservice" class="org.springframework.security.oauth2.provider.client.clientdetailsuserdetailsservice"> <constructor-arg ref="clientdetails" /> </bean> <bean id="clientdetails" class="com.example.myproject.service.clientservice"/> <authentication-manager id="userauthenticationmanager" xmlns="http://www.springframework.org/schema/security"> <authentication-provider user-service-ref="userservice"> </authentication-provider> </authentication-manager> <bean id="userservice" class="com.example.myproject.service.userservice"> </bean> <oauth:authorization-server client-details-service-ref="clientdetails" token-services-ref="tokenservices"> <oauth:authorization-code /> <oauth:implicit/> <oauth:refresh-token/> <oauth:client-credentials /> <oauth:password authentication-manager-ref="userauthenticationmanager"/> </oauth:authorization-server> <oauth:resource-server id="resourceserverfilter" resource-id="springsec" token-services-ref="tokenservices" /> <!-- <bean id="tokenstore" class="org.springframework.security.oauth2.provider.token.inmemorytokenstore" /> --> <bean id="tokenstore" class="org.springframework.security.oauth2.provider.token.store.inmemorytokenstore" > <property name="authenticationkeygenerator"> <bean class="com.example.myproject.service.uniqueauthenticationkeygenerator" /> </property> </bean> <bean id="tokenservices" class="org.springframework.security.oauth2.provider.token.defaulttokenservices"> <property name="tokenstore" ref="tokenstore" /> <property name="supportrefreshtoken" value="true" /> <property name="accesstokenvalidityseconds" value="300000"></property> <property name="clientdetailsservice" ref="clientdetails" /> <property name="tokenenhancer"><bean class="com.example.myproject.service.customtokenenhancer" /></property> </bean> <sec:global-method-security pre-post-annotations="enabled" proxy-target-class="true"> <!--you wire in expression handler @ layer of http filters. see https://jira.springsource.org/browse/sec-1452 --> <sec:expression-handler ref="oauthexpressionhandler" /> </sec:global-method-security> <oauth:expression-handler id="oauthexpressionhandler" /> <oauth:web-expression-handler id="oauthwebexpressionhandler" /> </beans>
clientservice.java :
@component public class clientservice implements clientdetailsservice { @autowired private oauthrepository oauthrepository; @override public clientdetails loadclientbyclientid(string s) throws clientregistrationexception{ baseclientdetails clientdetails = oauthrepository.getbyclientid(s); return clientdetails; } }
userservice.java :
@component public class userservice implements userdetailsservice { @autowired private oauthrepository oauthrepository; @override public userdetails loaduserbyusername(string s) throws usernamenotfoundexception { userdetails user = oauthrepository.getbyusername(s); return user; } }
oauthrepository.java :
@repository @transactional public class oauthrepository { @autowired private sessionfactory sessionfactory; @autowired private inmemorytokenstore tokenstore; private org.hibernate.session getcurrentsession(){ return sessionfactory.getcurrentsession(); } public userdetails getbyusername(string username) { myuser user=new myuser(); user.setusername(username); query query=getcurrentsession().createquery("from user username=:usrname"); query.setparameter("usrname", username); list<siuser> getuser=query.list(); user act=getuser.get(0); user.setpassword(act.getpassword()); user.setuserid(act.getuserid()); user.setauthorities(getauthorities(act.getuserid())); return user; } public baseclientdetails getbyclientid(string clientid) { system.out.println(" *** oauthrepository.getbyclientid "+clientid); query query=getcurrentsession().createquery("from oauthclientdetails clientid=:clientid"); query.setparameter("clientid", clientid); list<oauthclientdetails> getclient=query.list(); oauthclientdetails oauthclient=getclient.get(0); baseclientdetails details = new baseclientdetails(); details.setclientid(oauthclient.getclientid()); list<string> granttypeslist = arrays.aslist(oauthclient.getauthorizedgranttypes().split(",")); details.setauthorizedgranttypes(granttypeslist); details.setclientsecret(oauthclient.getclientsecret()); return details; } /** * retrieves collection of {@link grantedauthority} based on numerical role * @param role numerical role * @return collection of {@link grantedauthority */ public collection<grantedauthority> getauthorities(integer role) { list<grantedauthority> authlist = getgrantedauthorities(getroles(role)); return authlist; } /** * converts numerical role equivalent list of roles * @param role numerical role * @return list of roles as list of {@link string} */ public list<string> getroles(integer role) { list<string> roles = new arraylist<string>(); query query=getcurrentsession().createquery("from userrole userid=:userid"); query.setparameter("userid", role); list<siuserrole> getuser=query.list(); userrole actrole=getuser.get(0); roles.add(actrole.getrole()); return roles; } /** * wraps {@link string} roles {@link simplegrantedauthority} objects * @param roles {@link string} of roles * @return list of granted authorities */ public static list<grantedauthority> getgrantedauthorities(list<string> roles) { list<grantedauthority> authorities = new arraylist<grantedauthority>(); (string role : roles) { authorities.add(new grantedauthorityimpl(role)); } return authorities; } }
servlet-context.xml :
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- @author nagesh.chauhan(neel4soft@gmail.com) --> <context:annotation-config /> <context:component-scan base-package="com.example.myproject" /> <mvc:annotation-driven /> <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <!-- 1 of properties available; maximum file size in bytes --> <property name="maxuploadsize" value="1000000000" /> </bean> <bean id="mydatasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <property name="driverclassname" value="com.mysql.jdbc.driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/> <property name="username" value="username"/> <property name="password" value="password"/> <property name="validationquery" value="select 1"/> </bean> <!-- hibernate session factory --> <bean id="mysessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="datasource" ref="mydatasource"/> <property name="packagestoscan"> <array> <value>com.example.myproject</value> </array> </property> <property name="hibernateproperties"> <value> hibernate.dialect=org.hibernate.dialect.mysqldialect </value> </property> </bean> <!-- hibernate transaction manager --> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="sessionfactory" ref="mysessionfactory"/> </bean> <!-- activates annotation based transaction management --> <tx:annotation-driven transaction-manager="transactionmanager"/> </beans>
Comments
Post a Comment