java - How to determine on login page, if there is some user already logged? -


is there way, how determine, if there logged when using spring 3 mvc + spring security? security context:

<beans xmlns:security="http://www.springframework.org/schema/security"    xmlns="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.0.xsd              http://www.springframework.org/schema/security              http://www.springframework.org/schema/security/spring-security-3.1.xsd">  <security:http pattern="/resources/**" security="none"/> <security:http pattern="/login*" security="none" auto-config="true"/> <security:http pattern="/denied" security="none"/>  <security:http auto-config="true" access-denied-page="/denied" servlet-api-provision="false">     <security:intercept-url pattern="/login*" access="is_authenticated_anonymously"/>     <security:intercept-url pattern="/edit/**" access="role_edit"/>     <security:intercept-url pattern="/admin/**" access="role_admin"/>     <security:intercept-url pattern="/**" access="role_user"/>     <security:form-login login-page="/login"  authentication-failure-url="/denied"                          default-target-url="/"/>     <security:logout/> </security:http>  <security:authentication-manager>     <security:authentication-provider>         <security:user-service>             <security:user name="adam" password="adampassword" authorities="role_user"/>             <security:user name="jane" password="janepassword" authorities="role_user, role_admin"/>             <security:user name="sue" password="suepassword" authorities="role_user, role_edit"/>         </security:user-service>     </security:authentication-provider> </security:authentication-manager>  </beans> 

i can determine i.e. on home page via

<%     user user = (user) securitycontextholder.getcontext()             .getauthentication().getprincipal();     string username = user.getusername(); %> 

nevertheless on login page generates nullpointer exception.. :-/

and when there logged in , tries log in again, browser goes myurl.com/home page , 404 error, because right address should myurl.com/. when there nobody logged in, browser redirects right address. ideas can bug?

my jsp page:

<%@ page import="org.springframework.security.core.userdetails.user"%> <%@ page import="org.springframework.security.core.context.securitycontextholder"%> <%@ page import="java.util.collection"%> <%@ page import="javax.swing.text.abstractdocument"%> <%@ page import="org.springframework.security.core.grantedauthority"%>  <!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> </head> <body> <div class="form">     <form name="f" action="j_spring_security_check" method="post">         <input type="text" id="username" class="input-block-level"             name="j_username" placeholder="username"> <input             type="password" id="password" name="j_password"             class="input-block-level" placeholder="password">         <button class="btn btn-large btn-primary" type="submit">sign             in</button>     </form> </div> </body> </html> 

spring security contains jsp tag library, wich can imported next code:

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> 

with imported, can use jsp tag "sec" (for security),

 <sec:authorize access="isauthenticated()">      ... <!-- code or message show when user authenticated -->  </sec:authorize> 

within tag, can run code, display tags or html.

http://static.springsource.org/spring-security/site/docs/3.0.x/reference/taglibs.html


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -