]> juplo.de Git - website/blob
9231e1b657b463a06852b7e394330f76d2804b45
[website] /
1 ---
2 _edit_last: "2"
3 author: kai
4 categories:
5   - facebook
6   - java
7   - oauth2
8   - spring
9 date: "2016-06-26T10:40:45+00:00"
10 guid: http://juplo.de/?p=462
11 parent_post_id: null
12 post_id: "462"
13 title: Configure pac4j for a Social-Login along with a Spring-Security based Form-Login
14 url: /configure-pac4j-for-a-social-login-along-with-a-spring-security-based-form-login/
15
16 ---
17 ## The Problem – What will be explained
18
19 If you just want to enable your spring-based webapplication to let users log in with their social accounts, without changing anything else, [pac4j](http://www.pac4j.org/#1 "The authentication solution for java") should be your first choice.
20 But the [provided example](https://github.com/pac4j/spring-security-pac4j-demo "Clone the examples on GitHub") only shows, how to define all authentication mechanisms via pac4j.
21 If you already have set up your log-in via spring-security, you have to reconfigure it with the appropriate pac4j-mechanism.
22 That is a lot of unnecessary work, if you just want to supplement the already configured log in with the additionally possibility, to log in via a social provider.
23
24 In this short article, I will show you, how to set that up along with the normal [form-based login of Spring-Security](http://docs.spring.io/spring-security/site/docs/4.0.1.RELEASE/reference/htmlsingle/#ns-form-and-basic "Read, how to set up the form-based login of Spring-Security").
25 I will show this for a Login via Facabook along the Form-Login of Spring-Security.
26 The method should work as well for [other social logins, that are supported by spring-security-pac4j](https://github.com/pac4j/spring-security-pac4j#providers-supported "See a list of all login-mechanisms, supported by spring-security-pac4j"), along other login-mechanisms provided by spring-security out-of-the-box.
27
28 In this article I will not explain, how to store the user-profile-data, that was retrieved during the social login.
29 Also, if you need more social interaction, than just a login and access to the default data in the user-profile you probably need [spring-social](http://projects.spring.io/spring-social/ "Homepage of the spring-social project"). How to combine spring-social with spring-security for that purpose, is explained in this nice article about how to [add social sign in to a spring-mvc weba-pplication](http://www.petrikainulainen.net/programming/spring-framework/adding-social-sign-in-to-a-spring-mvc-web-application-configuration/ "Read this article about how to integrate spring-security with spring-social").
30
31 ## Adding the Required Maven-Artifacts
32
33 In order to use spring-security-pac4j to login to facebook, you need the following maven-artifacts:
34
35 ```xml
36
37 <dependency>
38   <groupId>org.pac4j</groupId>
39   <artifactId>spring-security-pac4j</artifactId>
40   <version>1.2.5</version>
41 </dependency>
42 <dependency>
43   <groupId>org.pac4j</groupId>
44   <artifactId>pac4j-http</artifactId>
45   <version>1.7.1</version>
46 </dependency>
47 <dependency>
48   <groupId>org.pac4j</groupId>
49   <artifactId>pac4j-oauth</artifactId>
50   <version>1.7.1</version>
51 </dependency>
52
53 ```
54
55 ## Configuration of Spring-Security (Without Social Login via pac4j)
56
57 This is a bare minimal configuration to get the form-login via Spring-Security working:
58
59 ```xml
60
61 <?xml version="1.0" encoding="UTF-8"?>
62 <beans
63     xmlns="http://www.springframework.org/schema/beans"
64     xmlns:security="http://www.springframework.org/schema/security"
65     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
66     xsi:schemaLocation="
67       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
68       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
69     ">
70
71   <security:http use-expressions="true">
72     <security:intercept-url pattern="/**" access="permitAll"/>
73     <security:intercept-url pattern="/home.html" access="isAuthenticated()"/>
74     <security:form-login login-page="/login.html" authentication-failure-url="/login.html?failure"/>
75     <security:logout/>
76     <security:remember-me/>
77   </security:http>
78
79   <security:authentication-manager>
80     <security:authentication-provider>
81       <security:user-service>
82         <security:user name="user" password="user" authorities="ROLE_USER" />
83       </security:user-service>
84     </security:authentication-provider>
85   </security:authentication-manager>
86
87 </beans>
88
89 ```
90
91 The `http` defines, that the access to the url `/home.html` is restriced and must be authenticated via a form-login on url `/login.html`.
92 The `authentication-manager` defines an in-memory authentication-provider for testing purposes with just one user (username: `user`, password: `user`).
93 For more details, see the [documentation of spring-security](http://docs.spring.io/spring-security/site/docs/4.0.1.RELEASE/reference/htmlsingle/#ns-form-and-basic "Read more about the available configuration-parameters in the spring-security documentation").
94
95 ## Enabling pac4j via spring-security-pac4j alongside
96
97 To enable pac4j alongside, you have to add/change the following:
98
99 ```xml
100
101 <?xml version="1.0" encoding="UTF-8"?>
102 <beans
103     xmlns="http://www.springframework.org/schema/beans"
104     xmlns:security="http://www.springframework.org/schema/security"
105     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
106     xsi:schemaLocation="
107       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
108       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
109     ">
110
111   <security:http use-expressions="true">
112     <security:custom-filter position="OPENID_FILTER" ref="clientFilter"/>
113     <security:intercept-url pattern="/**" access="permitAll()"/>
114     <security:intercept-url pattern="/home.html" access="isAuthenticated()"/>
115     <security:form-login login-page="/login.html" authentication-failure-url="/login.html?failure"/>
116     <security:logout/>
117   </security:http>
118
119   <security:authentication-manager alias="authenticationManager">
120     <security:authentication-provider>
121       <security:user-service>
122         <security:user name="user" password="user" authorities="ROLE_USER" />
123       </security:user-service>
124     </security:authentication-provider>
125     <security:authentication-provider ref="clientProvider"/>
126   </security:authentication-manager>
127
128   <!-- entry points -->
129   <bean id="facebookEntryPoint" class="org.pac4j.springframework.security.web.ClientAuthenticationEntryPoint">
130     <property name="client" ref="facebookClient"/>
131   </bean>
132
133   <!-- client definitions -->
134   <bean id="facebookClient" class="org.pac4j.oauth.client.FacebookClient">
135     <property name="key" value="145278422258960"/>
136     <property name="secret" value="be21409ba8f39b5dae2a7de525484da8"/>
137   </bean>
138   <bean id="clients" class="org.pac4j.core.client.Clients">
139     <property name="callbackUrl" value="http://localhost:8080/callback"/>
140     <property name="clients">
141       <list>
142         <ref bean="facebookClient"/>
143       </list>
144     </property>
145   </bean>
146
147   <!-- common to all clients -->
148   <bean id="clientFilter" class="org.pac4j.springframework.security.web.ClientAuthenticationFilter">
149     <constructor-arg value="/callback"/>
150     <property name="clients" ref="clients"/>
151     <property name="sessionAuthenticationStrategy" ref="sas"/>
152     <property name="authenticationManager" ref="authenticationManager"/>
153   </bean>
154   <bean id="clientProvider" class="org.pac4j.springframework.security.authentication.ClientAuthenticationProvider">
155     <property name="clients" ref="clients"/>
156   </bean>
157   <bean id="httpSessionRequestCache" class="org.springframework.security.web.savedrequest.HttpSessionRequestCache"/>
158   <bean id="sas" class="org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy"/>
159
160 </beans>
161
162 ```
163
164 In short:
165
166 1. You have to add an additional filter in `http`.
167    I added this filter on position `OPENID_FILTER`, because pac4j introduces a unified way to handle OpenID and OAuth and so on.
168    If you are using the OpenID-mechanism of spring-security, you have to use another position in the filter-chain (for example `CAS_FILTER`) or reconfigure OpenID to use the pac4j-mechanism, which should be fairly straight-forward.
169
170
171    The new Filter has the ID `clientFilter` and needs a reference to the `authenticationManager`.
172    Also, the callback-URL (here: `/callback`) must be mapped to your web-application!
173
174 1. You have to add an additional `authentication-provider` to the `authentication-manager`, that references your newly defined pac4j-ClientProvider ( `clientProvider`).
175
176 1. You have to configure your entry-points as pac4j-clients.
177    In the example above, only one pac4j-client, that authenticats the user via Facebook, is configured.
178    You easily can add more clients: just copy the definitions from the [spring-security-pac4j example](https://github.com/pac4j/spring-security-pac4j-demo "Browse the source of that example on GitHub").
179
180 That should be all, that is necessary, to enable a Facebook-Login in your Spring-Security web-application.
181
182 ## Do Not Forget To Use Your Own APP-ID!
183
184 The App-ID `145278422258960` and the accompanying secret `be21409ba8f39b5dae2a7de525484da8` were taken from the [spring-security-pac4j example](https://github.com/pac4j/spring-security-pac4j-demo "Browse the source of that example on GitHub") for simplicity.
185 That works for a first test-run on `localhost`.
186 _But you have to replace that with your own App-ID and -scecret, that you have to generate using [your App Dashboard on Facebook](https://developers.facebook.com/apps "You can generate your own apps on your App Dashboard")!_
187
188 ## More to come...
189
190 This short article does not show, how to save the retrieved user-profiles in your user-database, if you need that.
191 I hope, I will write a follow-up on that soon.
192 In short:
193 pac4j creates a Spring-Security `UserDetails`-Instance for every user, that was authenticated against it.
194 You can use this, to access the data in the retrieved user-profile (for example to write out the name of the user in a greeting or contact him via e-mail).