Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Security and logging in from another site without reentering credentials
    primarykey
    data
    text
    <p>As the title of the post says, I'm integrating Spring Security in my already working Spring MVC web app and I need that other site's users can open a link from that site to mine and be already logged in, without the hassle of reentering the user name and the password and without causing the other web site's developer to change too many things. The users will always log in first on the other site and then come to mine, but never the other way around. Both web applications are for internal use of a single company, with no access to the hosting servers from any outsider network, and both sites can access the same user database. Despite being this scenario quite secure (not public web sites) I need the solution to be as safe as possible. There are several approches I've seen after searching for some time:</p> <ol> <li>Single Sign On solution, being CAS the most mentioned. However, all examples of this that I've seen so far require the user to reenter credentials, which I want to avoid.</li> <li>Use a token generator in a three-step-handshaking process involving web services, like stated in this post <a href="https://stackoverflow.com/questions/351873/passing-credentials-between-sites">Passing credentials between sites</a></li> <li>Encrypt user and password credentials and send them through the URL itself</li> </ol> <p>As I told you I need the safest and simplest way to do this, considering the above explanations. I would also appreciate some code about the solution (mostly bean configuration in Spring Security and services/java classes to deal with this).</p> <p>Thanks!!</p> <p><strong>EDIT :</strong> I found a different and simpler approach to solve my particular case. The main web page where users log in can redirect to mine doing a POST request that should include a parameter "login". Then I only have to get this parameter and force this user to be authenticated within the security context. I'll post the code but I'm still having one issue, just keep reading till the end:</p> <p>Web.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/applicationContext.xml /WEB-INF/applicationContext-security.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;context-param&gt; &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/log4j.properties&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;session-config&gt; &lt;session-timeout&gt; 1 &lt;/session-timeout&gt; &lt;/session-config&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;redirect.jsp&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;!-- Spring Security --&gt; &lt;filter&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;filter-class&gt; org.springframework.web.filter.DelegatingFilterProxy &lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;/web-app&gt; </code></pre> <p>applicationContext-security.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"&gt; &lt;!-- This is where we configure Spring-Security --&gt; &lt;http use-expressions="true"&gt; &lt;intercept-url pattern="/ShowDocuments" access="permitAll"/&gt; &lt;intercept-url pattern="/AddDocForm" access="hasRole('ROLE_ADMIN')"/&gt; &lt;access-denied-handler error-page="/AccessDenied"/&gt; &lt;http-basic/&gt; &lt;session-management&gt; &lt;concurrency-control expired-url="/sessionExpired.html"/&gt; &lt;/session-management&gt; &lt;/http&gt; &lt;authentication-manager&gt;&lt;/authentication-manager&gt; &lt;/beans:beans&gt; </code></pre> <p>As you can see the AddDocForm is only accessible for admin users. In order to test if all of this works I need some way to force the security context to have any user authenticated. This is the java code I've used:</p> <pre><code>public boolean loginUser() { boolean success = true; List&lt;GrantedAuthority&gt; authorities = AuthorityUtils.createAuthorityList("ROLE_USER"); UserDetails userDetails = new User(usuario.getLogin(), "undefined", authorities); Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities()); SecurityContextHolder.getContext().setAuthentication(authentication); return success; }//END_METHOD </code></pre> <p>This method will authenticate one user with authority ROLE_USER. Therefore, access to AddDocForm shouldn't be allowed. This is the procedure I follow:</p> <ol> <li>I load the main page as if it was loaded from another site.</li> <li>The controller that handles this performs a ROLE_USER authentication before loading the main page.</li> <li>From there I try to access AddDocForm and.... yes, the AccessDenied page is shown.</li> </ol> <p>However, whenever the user's session expires (or at least that's what I think is happening) my Firefox browser araises a credentials pop up window like this: <img src="https://i.stack.imgur.com/vSiti.png" alt="credetentials pop-up window"></p> <p>As I didn't want this to happen I added the <code>&lt;session-management&gt; &lt;concurrency-control expired-url="/sessionExpired.html"/&gt;&lt;/session-management&gt;</code> line of code to avoid it, but keeps happening.</p> <p>Does anybody have a clue on this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload