From 98890ad2319bfe73a6e2461753a7d65ad5d7b1f2 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 11 Aug 2013 13:33:52 +0300 Subject: [PATCH] separating the session work into it's own example project --- spring-security-mvc-custom/README.md | 5 +- spring-security-mvc-session/.classpath | 31 +++ ...e.wst.jsdt.core.javascriptValidator.launch | 7 + spring-security-mvc-session/.gitignore | 13 ++ spring-security-mvc-session/.project | 42 ++++ .../.settings/.jsdtscope | 5 + .../.settings/org.eclipse.jdt.core.prefs | 95 ++++++++ .../.settings/org.eclipse.jdt.ui.prefs | 55 +++++ .../.settings/org.eclipse.m2e.core.prefs | 4 + .../.settings/org.eclipse.m2e.wtp.prefs | 2 + .../org.eclipse.wst.common.component | 10 + ....eclipse.wst.common.project.facet.core.xml | 5 + ...rg.eclipse.wst.jsdt.ui.superType.container | 1 + .../org.eclipse.wst.jsdt.ui.superType.name | 1 + .../org.eclipse.wst.validation.prefs | 14 ++ .../org.eclipse.wst.ws.service.policy.prefs | 2 + spring-security-mvc-session/.springBeans | 14 ++ spring-security-mvc-session/README.md | 15 ++ spring-security-mvc-session/pom.xml | 220 ++++++++++++++++++ .../monitoring/MetricRegistrySingleton.java | 25 ++ ...SimpleUrlAuthenticationSuccessHandler.java | 94 ++++++++ .../java/org/baeldung/spring/MvcConfig.java | 43 ++++ .../baeldung/spring/SecSecurityConfig.java | 14 ++ .../web/SessionListenerWithMetrics.java | 43 ++++ .../src/main/resources/logback.xml | 20 ++ .../src/main/resources/webSecurityConfig.xml | 33 +++ .../src/main/webapp/WEB-INF/mvc-servlet.xml | 6 + .../main/webapp/WEB-INF/view/anonymous.jsp | 10 + .../src/main/webapp/WEB-INF/view/console.jsp | 22 ++ .../src/main/webapp/WEB-INF/view/homepage.jsp | 22 ++ .../src/main/webapp/WEB-INF/view/login.jsp | 30 +++ .../src/main/webapp/WEB-INF/web.xml | 57 +++++ .../src/test/resources/.gitignore | 13 ++ 33 files changed, 971 insertions(+), 2 deletions(-) create mode 100644 spring-security-mvc-session/.classpath create mode 100644 spring-security-mvc-session/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch create mode 100644 spring-security-mvc-session/.gitignore create mode 100644 spring-security-mvc-session/.project create mode 100644 spring-security-mvc-session/.settings/.jsdtscope create mode 100644 spring-security-mvc-session/.settings/org.eclipse.jdt.core.prefs create mode 100644 spring-security-mvc-session/.settings/org.eclipse.jdt.ui.prefs create mode 100644 spring-security-mvc-session/.settings/org.eclipse.m2e.core.prefs create mode 100644 spring-security-mvc-session/.settings/org.eclipse.m2e.wtp.prefs create mode 100644 spring-security-mvc-session/.settings/org.eclipse.wst.common.component create mode 100644 spring-security-mvc-session/.settings/org.eclipse.wst.common.project.facet.core.xml create mode 100644 spring-security-mvc-session/.settings/org.eclipse.wst.jsdt.ui.superType.container create mode 100644 spring-security-mvc-session/.settings/org.eclipse.wst.jsdt.ui.superType.name create mode 100644 spring-security-mvc-session/.settings/org.eclipse.wst.validation.prefs create mode 100644 spring-security-mvc-session/.settings/org.eclipse.wst.ws.service.policy.prefs create mode 100644 spring-security-mvc-session/.springBeans create mode 100644 spring-security-mvc-session/README.md create mode 100644 spring-security-mvc-session/pom.xml create mode 100644 spring-security-mvc-session/src/main/java/org/baeldung/monitoring/MetricRegistrySingleton.java create mode 100644 spring-security-mvc-session/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java create mode 100644 spring-security-mvc-session/src/main/java/org/baeldung/spring/MvcConfig.java create mode 100644 spring-security-mvc-session/src/main/java/org/baeldung/spring/SecSecurityConfig.java create mode 100644 spring-security-mvc-session/src/main/java/org/baeldung/web/SessionListenerWithMetrics.java create mode 100644 spring-security-mvc-session/src/main/resources/logback.xml create mode 100644 spring-security-mvc-session/src/main/resources/webSecurityConfig.xml create mode 100644 spring-security-mvc-session/src/main/webapp/WEB-INF/mvc-servlet.xml create mode 100644 spring-security-mvc-session/src/main/webapp/WEB-INF/view/anonymous.jsp create mode 100644 spring-security-mvc-session/src/main/webapp/WEB-INF/view/console.jsp create mode 100644 spring-security-mvc-session/src/main/webapp/WEB-INF/view/homepage.jsp create mode 100644 spring-security-mvc-session/src/main/webapp/WEB-INF/view/login.jsp create mode 100644 spring-security-mvc-session/src/main/webapp/WEB-INF/web.xml create mode 100644 spring-security-mvc-session/src/test/resources/.gitignore diff --git a/spring-security-mvc-custom/README.md b/spring-security-mvc-custom/README.md index ea03f5664e..53bdac770c 100644 --- a/spring-security-mvc-custom/README.md +++ b/spring-security-mvc-custom/README.md @@ -1,11 +1,12 @@ ========= ## Spring Security Login Example Project -- [Spring Security Remember Me](http://www.baeldung.com/spring-security-remember-me) -- [Redirect to different pages after Login with Spring Security](http://www.baeldung.com/spring_redirect_after_login) ### Relevant Articles: +- [Spring Security Remember Me](http://www.baeldung.com/spring-security-remember-me) +- [Redirect to different pages after Login with Spring Security](http://www.baeldung.com/spring_redirect_after_login) +- [HttpSessionListener Example – Monitoring](http://www.baeldung.com/httpsessionlistener_with_metrics) ### Build the Project diff --git a/spring-security-mvc-session/.classpath b/spring-security-mvc-session/.classpath new file mode 100644 index 0000000000..5dea193677 --- /dev/null +++ b/spring-security-mvc-session/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-security-mvc-session/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch b/spring-security-mvc-session/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch new file mode 100644 index 0000000000..627021fb96 --- /dev/null +++ b/spring-security-mvc-session/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch @@ -0,0 +1,7 @@ + + + + + + + diff --git a/spring-security-mvc-session/.gitignore b/spring-security-mvc-session/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/spring-security-mvc-session/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-security-mvc-session/.project b/spring-security-mvc-session/.project new file mode 100644 index 0000000000..4670a72a3b --- /dev/null +++ b/spring-security-mvc-session/.project @@ -0,0 +1,42 @@ + + + spring-security-mvc-session + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/spring-security-mvc-session/.settings/.jsdtscope b/spring-security-mvc-session/.settings/.jsdtscope new file mode 100644 index 0000000000..7b3f0c8b9f --- /dev/null +++ b/spring-security-mvc-session/.settings/.jsdtscope @@ -0,0 +1,5 @@ + + + + + diff --git a/spring-security-mvc-session/.settings/org.eclipse.jdt.core.prefs b/spring-security-mvc-session/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..723e5b1245 --- /dev/null +++ b/spring-security-mvc-session/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,95 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore +org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull +org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault +org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable +org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.autoboxing=ignore +org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning +org.eclipse.jdt.core.compiler.problem.deadCode=warning +org.eclipse.jdt.core.compiler.problem.deprecation=warning +org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled +org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled +org.eclipse.jdt.core.compiler.problem.discouragedReference=warning +org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore +org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore +org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled +org.eclipse.jdt.core.compiler.problem.fieldHiding=error +org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning +org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning +org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled +org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning +org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore +org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore +org.eclipse.jdt.core.compiler.problem.localVariableHiding=error +org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning +org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore +org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore +org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled +org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore +org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore +org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled +org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning +org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore +org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning +org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning +org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore +org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error +org.eclipse.jdt.core.compiler.problem.nullReference=warning +org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error +org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning +org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning +org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore +org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore +org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore +org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore +org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning +org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning +org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore +org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore +org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore +org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore +org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore +org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled +org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning +org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled +org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled +org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore +org.eclipse.jdt.core.compiler.problem.typeParameterHiding=error +org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled +org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning +org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning +org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore +org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning +org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore +org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore +org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled +org.eclipse.jdt.core.compiler.problem.unusedImport=warning +org.eclipse.jdt.core.compiler.problem.unusedLabel=warning +org.eclipse.jdt.core.compiler.problem.unusedLocal=warning +org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore +org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore +org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled +org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled +org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled +org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning +org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning +org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/spring-security-mvc-session/.settings/org.eclipse.jdt.ui.prefs b/spring-security-mvc-session/.settings/org.eclipse.jdt.ui.prefs new file mode 100644 index 0000000000..471e9b0d81 --- /dev/null +++ b/spring-security-mvc-session/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,55 @@ +#Sat Jan 21 23:04:06 EET 2012 +eclipse.preferences.version=1 +editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true +sp_cleanup.add_default_serial_version_id=true +sp_cleanup.add_generated_serial_version_id=false +sp_cleanup.add_missing_annotations=true +sp_cleanup.add_missing_deprecated_annotations=true +sp_cleanup.add_missing_methods=false +sp_cleanup.add_missing_nls_tags=false +sp_cleanup.add_missing_override_annotations=true +sp_cleanup.add_missing_override_annotations_interface_methods=true +sp_cleanup.add_serial_version_id=false +sp_cleanup.always_use_blocks=true +sp_cleanup.always_use_parentheses_in_expressions=true +sp_cleanup.always_use_this_for_non_static_field_access=false +sp_cleanup.always_use_this_for_non_static_method_access=false +sp_cleanup.convert_to_enhanced_for_loop=true +sp_cleanup.correct_indentation=true +sp_cleanup.format_source_code=true +sp_cleanup.format_source_code_changes_only=true +sp_cleanup.make_local_variable_final=true +sp_cleanup.make_parameters_final=true +sp_cleanup.make_private_fields_final=false +sp_cleanup.make_type_abstract_if_missing_method=false +sp_cleanup.make_variable_declarations_final=true +sp_cleanup.never_use_blocks=false +sp_cleanup.never_use_parentheses_in_expressions=false +sp_cleanup.on_save_use_additional_actions=true +sp_cleanup.organize_imports=true +sp_cleanup.qualify_static_field_accesses_with_declaring_class=false +sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true +sp_cleanup.qualify_static_member_accesses_with_declaring_class=true +sp_cleanup.qualify_static_method_accesses_with_declaring_class=false +sp_cleanup.remove_private_constructors=true +sp_cleanup.remove_trailing_whitespaces=true +sp_cleanup.remove_trailing_whitespaces_all=true +sp_cleanup.remove_trailing_whitespaces_ignore_empty=false +sp_cleanup.remove_unnecessary_casts=true +sp_cleanup.remove_unnecessary_nls_tags=false +sp_cleanup.remove_unused_imports=true +sp_cleanup.remove_unused_local_variables=false +sp_cleanup.remove_unused_private_fields=true +sp_cleanup.remove_unused_private_members=false +sp_cleanup.remove_unused_private_methods=true +sp_cleanup.remove_unused_private_types=true +sp_cleanup.sort_members=false +sp_cleanup.sort_members_all=false +sp_cleanup.use_blocks=false +sp_cleanup.use_blocks_only_for_return_and_throw=false +sp_cleanup.use_parentheses_in_expressions=false +sp_cleanup.use_this_for_non_static_field_access=true +sp_cleanup.use_this_for_non_static_field_access_only_if_necessary=true +sp_cleanup.use_this_for_non_static_method_access=true +sp_cleanup.use_this_for_non_static_method_access_only_if_necessary=true diff --git a/spring-security-mvc-session/.settings/org.eclipse.m2e.core.prefs b/spring-security-mvc-session/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..f897a7f1cb --- /dev/null +++ b/spring-security-mvc-session/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/spring-security-mvc-session/.settings/org.eclipse.m2e.wtp.prefs b/spring-security-mvc-session/.settings/org.eclipse.m2e.wtp.prefs new file mode 100644 index 0000000000..ef86089622 --- /dev/null +++ b/spring-security-mvc-session/.settings/org.eclipse.m2e.wtp.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.m2e.wtp.enabledProjectSpecificPrefs=false diff --git a/spring-security-mvc-session/.settings/org.eclipse.wst.common.component b/spring-security-mvc-session/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000000..0a268a3d82 --- /dev/null +++ b/spring-security-mvc-session/.settings/org.eclipse.wst.common.component @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/spring-security-mvc-session/.settings/org.eclipse.wst.common.project.facet.core.xml b/spring-security-mvc-session/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000000..b46bc511b9 --- /dev/null +++ b/spring-security-mvc-session/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/spring-security-mvc-session/.settings/org.eclipse.wst.jsdt.ui.superType.container b/spring-security-mvc-session/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000000..3bd5d0a480 --- /dev/null +++ b/spring-security-mvc-session/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/spring-security-mvc-session/.settings/org.eclipse.wst.jsdt.ui.superType.name b/spring-security-mvc-session/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 0000000000..05bd71b6ec --- /dev/null +++ b/spring-security-mvc-session/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Window \ No newline at end of file diff --git a/spring-security-mvc-session/.settings/org.eclipse.wst.validation.prefs b/spring-security-mvc-session/.settings/org.eclipse.wst.validation.prefs new file mode 100644 index 0000000000..cacf5451ae --- /dev/null +++ b/spring-security-mvc-session/.settings/org.eclipse.wst.validation.prefs @@ -0,0 +1,14 @@ +DELEGATES_PREFERENCE=delegateValidatorList +USER_BUILD_PREFERENCE=enabledBuildValidatorListorg.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator; +USER_MANUAL_PREFERENCE=enabledManualValidatorListorg.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator; +USER_PREFERENCE=overrideGlobalPreferencestruedisableAllValidationfalseversion1.2.303.v201202090300 +eclipse.preferences.version=1 +override=true +suspend=false +vals/org.eclipse.jst.jsf.ui.JSFAppConfigValidator/global=FF01 +vals/org.eclipse.jst.jsp.core.JSPBatchValidator/global=FF01 +vals/org.eclipse.jst.jsp.core.JSPContentValidator/global=FF01 +vals/org.eclipse.jst.jsp.core.TLDValidator/global=FF01 +vals/org.eclipse.wst.dtd.core.dtdDTDValidator/global=FF01 +vals/org.eclipse.wst.jsdt.web.core.JsBatchValidator/global=TF02 +vf.version=3 diff --git a/spring-security-mvc-session/.settings/org.eclipse.wst.ws.service.policy.prefs b/spring-security-mvc-session/.settings/org.eclipse.wst.ws.service.policy.prefs new file mode 100644 index 0000000000..9cfcabe16f --- /dev/null +++ b/spring-security-mvc-session/.settings/org.eclipse.wst.ws.service.policy.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.wst.ws.service.policy.projectEnabled=false diff --git a/spring-security-mvc-session/.springBeans b/spring-security-mvc-session/.springBeans new file mode 100644 index 0000000000..7623a7e888 --- /dev/null +++ b/spring-security-mvc-session/.springBeans @@ -0,0 +1,14 @@ + + + 1 + + + + + + + src/main/webapp/WEB-INF/mvc-servlet.xml + + + + diff --git a/spring-security-mvc-session/README.md b/spring-security-mvc-session/README.md new file mode 100644 index 0000000000..53bdac770c --- /dev/null +++ b/spring-security-mvc-session/README.md @@ -0,0 +1,15 @@ +========= + +## Spring Security Login Example Project + + +### Relevant Articles: +- [Spring Security Remember Me](http://www.baeldung.com/spring-security-remember-me) +- [Redirect to different pages after Login with Spring Security](http://www.baeldung.com/spring_redirect_after_login) +- [HttpSessionListener Example – Monitoring](http://www.baeldung.com/httpsessionlistener_with_metrics) + + +### Build the Project +``` +mvn clean install +``` diff --git a/spring-security-mvc-session/pom.xml b/spring-security-mvc-session/pom.xml new file mode 100644 index 0000000000..7d7588cd19 --- /dev/null +++ b/spring-security-mvc-session/pom.xml @@ -0,0 +1,220 @@ + + 4.0.0 + org.baeldung + spring-security-mvc-custom + 0.1-SNAPSHOT + + spring-security-mvc-custom + war + + + + + + + org.springframework.security + spring-security-web + ${org.springframework.security.version} + + + org.springframework.security + spring-security-config + ${org.springframework.security.version} + + + org.springframework.security + spring-security-taglibs + ${org.springframework.security.version} + + + + + + org.springframework + spring-core + ${org.springframework.version} + + + org.springframework + spring-context + ${org.springframework.version} + + + org.springframework + spring-jdbc + ${org.springframework.version} + + + org.springframework + spring-beans + ${org.springframework.version} + + + org.springframework + spring-aop + ${org.springframework.version} + + + org.springframework + spring-tx + ${org.springframework.version} + + + org.springframework + spring-expression + ${org.springframework.version} + + + + org.springframework + spring-web + ${org.springframework.version} + + + org.springframework + spring-webmvc + ${org.springframework.version} + + + + + + javax.servlet + javax.servlet-api + 3.0.1 + provided + + + + javax.servlet + jstl + 1.2 + runtime + + + + + + com.codahale.metrics + metrics-core + 3.0.1 + + + + + + junit + junit-dep + ${junit.version} + test + + + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + + + spring-security-mvc-custom + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + + + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + jetty8x + embedded + + + + + + + 8082 + + + + + + + + + + + + 3.2.4.RELEASE + 3.1.4.RELEASE + + + 4.2.3.Final + 5.1.26 + + + 1.7.5 + 1.0.11 + + + 5.0.1.Final + + + 14.0.1 + 3.1 + + + 1.3 + 4.11 + 1.9.5 + + 4.2.5 + 4.2.4 + + 1.8.1 + 1.8.9 + + + 1.4.3 + 2.15 + + + \ No newline at end of file diff --git a/spring-security-mvc-session/src/main/java/org/baeldung/monitoring/MetricRegistrySingleton.java b/spring-security-mvc-session/src/main/java/org/baeldung/monitoring/MetricRegistrySingleton.java new file mode 100644 index 0000000000..ed253305ed --- /dev/null +++ b/spring-security-mvc-session/src/main/java/org/baeldung/monitoring/MetricRegistrySingleton.java @@ -0,0 +1,25 @@ +package org.baeldung.monitoring; + +import java.util.concurrent.TimeUnit; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Slf4jReporter; + +public final class MetricRegistrySingleton { + + public static final MetricRegistry metrics = new MetricRegistry(); + + static { + Logger logger = LoggerFactory.getLogger("org.baeldung.monitoring"); + final Slf4jReporter reporter = Slf4jReporter.forRegistry(metrics).outputTo(logger).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build(); + reporter.start(5, TimeUnit.MINUTES); + } + + private MetricRegistrySingleton() { + throw new AssertionError(); + } + +} diff --git a/spring-security-mvc-session/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java b/spring-security-mvc-session/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java new file mode 100644 index 0000000000..19f1ca76a6 --- /dev/null +++ b/spring-security-mvc-session/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java @@ -0,0 +1,94 @@ +package org.baeldung.security; + +import java.io.IOException; +import java.util.Collection; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.web.DefaultRedirectStrategy; +import org.springframework.security.web.RedirectStrategy; +import org.springframework.security.web.WebAttributes; +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; + +public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler { + protected final Log logger = LogFactory.getLog(this.getClass()); + + private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); + + protected MySimpleUrlAuthenticationSuccessHandler() { + super(); + } + + // API + + @Override + public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException { + handle(request, response, authentication); + clearAuthenticationAttributes(request); + } + + // IMPL + + protected void handle(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException { + final String targetUrl = determineTargetUrl(authentication); + + if (response.isCommitted()) { + logger.debug("Response has already been committed. Unable to redirect to " + targetUrl); + return; + } + + redirectStrategy.sendRedirect(request, response, targetUrl); + } + + protected String determineTargetUrl(final Authentication authentication) { + boolean isUser = false; + boolean isAdmin = false; + final Collection authorities = authentication.getAuthorities(); + for (final GrantedAuthority grantedAuthority : authorities) { + if (grantedAuthority.getAuthority().equals("ROLE_USER")) { + isUser = true; + break; + } else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) { + isAdmin = true; + break; + } + } + + if (isUser) { + return "/homepage.html"; + } else if (isAdmin) { + return "/console.html"; + } else { + throw new IllegalStateException(); + } + } + + /** + * Removes temporary authentication-related data which may have been stored in the session + * during the authentication process. + */ + protected final void clearAuthenticationAttributes(final HttpServletRequest request) { + final HttpSession session = request.getSession(false); + + if (session == null) { + return; + } + + session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION); + } + + public void setRedirectStrategy(final RedirectStrategy redirectStrategy) { + this.redirectStrategy = redirectStrategy; + } + + protected RedirectStrategy getRedirectStrategy() { + return redirectStrategy; + } + +} diff --git a/spring-security-mvc-session/src/main/java/org/baeldung/spring/MvcConfig.java b/spring-security-mvc-session/src/main/java/org/baeldung/spring/MvcConfig.java new file mode 100644 index 0000000000..2229516633 --- /dev/null +++ b/spring-security-mvc-session/src/main/java/org/baeldung/spring/MvcConfig.java @@ -0,0 +1,43 @@ +package org.baeldung.spring; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; + +@EnableWebMvc +@Configuration +public class MvcConfig extends WebMvcConfigurerAdapter { + + public MvcConfig() { + super(); + } + + // API + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); + + registry.addViewController("/anonymous.html"); + + registry.addViewController("/login.html"); + registry.addViewController("/homepage.html"); + registry.addViewController("/console.html"); + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + + return bean; + } +} \ No newline at end of file diff --git a/spring-security-mvc-session/src/main/java/org/baeldung/spring/SecSecurityConfig.java b/spring-security-mvc-session/src/main/java/org/baeldung/spring/SecSecurityConfig.java new file mode 100644 index 0000000000..4da114c78b --- /dev/null +++ b/spring-security-mvc-session/src/main/java/org/baeldung/spring/SecSecurityConfig.java @@ -0,0 +1,14 @@ +package org.baeldung.spring; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +@Configuration +@ImportResource({ "classpath:webSecurityConfig.xml" }) +public class SecSecurityConfig { + + public SecSecurityConfig() { + super(); + } + +} diff --git a/spring-security-mvc-session/src/main/java/org/baeldung/web/SessionListenerWithMetrics.java b/spring-security-mvc-session/src/main/java/org/baeldung/web/SessionListenerWithMetrics.java new file mode 100644 index 0000000000..46bf2708f7 --- /dev/null +++ b/spring-security-mvc-session/src/main/java/org/baeldung/web/SessionListenerWithMetrics.java @@ -0,0 +1,43 @@ +package org.baeldung.web; + +import java.util.concurrent.atomic.AtomicInteger; + +import javax.servlet.http.HttpSessionEvent; +import javax.servlet.http.HttpSessionListener; + +import org.baeldung.monitoring.MetricRegistrySingleton; + +import com.codahale.metrics.Counter; + +public class SessionListenerWithMetrics implements HttpSessionListener { + + private final AtomicInteger activeSessions; + + private final Counter counterOfActiveSessions; + + public SessionListenerWithMetrics() { + super(); + + activeSessions = new AtomicInteger(); + counterOfActiveSessions = MetricRegistrySingleton.metrics.counter("web.sessions.active.count"); + } + + // API + + public final int getTotalActiveSession() { + return activeSessions.get(); + } + + @Override + public final void sessionCreated(final HttpSessionEvent event) { + activeSessions.incrementAndGet(); + counterOfActiveSessions.inc(); + } + + @Override + public final void sessionDestroyed(final HttpSessionEvent event) { + activeSessions.decrementAndGet(); + counterOfActiveSessions.dec(); + } + +} \ No newline at end of file diff --git a/spring-security-mvc-session/src/main/resources/logback.xml b/spring-security-mvc-session/src/main/resources/logback.xml new file mode 100644 index 0000000000..1146dade63 --- /dev/null +++ b/spring-security-mvc-session/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-security-mvc-session/src/main/resources/webSecurityConfig.xml b/spring-security-mvc-session/src/main/resources/webSecurityConfig.xml new file mode 100644 index 0000000000..186a435cdb --- /dev/null +++ b/spring-security-mvc-session/src/main/resources/webSecurityConfig.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-security-mvc-session/src/main/webapp/WEB-INF/mvc-servlet.xml b/spring-security-mvc-session/src/main/webapp/WEB-INF/mvc-servlet.xml new file mode 100644 index 0000000000..5463820874 --- /dev/null +++ b/spring-security-mvc-session/src/main/webapp/WEB-INF/mvc-servlet.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/spring-security-mvc-session/src/main/webapp/WEB-INF/view/anonymous.jsp b/spring-security-mvc-session/src/main/webapp/WEB-INF/view/anonymous.jsp new file mode 100644 index 0000000000..d4e9c0289b --- /dev/null +++ b/spring-security-mvc-session/src/main/webapp/WEB-INF/view/anonymous.jsp @@ -0,0 +1,10 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + + + + +

Anonymous page

+ + ">To Login + + \ No newline at end of file diff --git a/spring-security-mvc-session/src/main/webapp/WEB-INF/view/console.jsp b/spring-security-mvc-session/src/main/webapp/WEB-INF/view/console.jsp new file mode 100644 index 0000000000..d18b59a10c --- /dev/null +++ b/spring-security-mvc-session/src/main/webapp/WEB-INF/view/console.jsp @@ -0,0 +1,22 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %> + + + + +

This is the landing page for the admin

+ + + This text is only visible to a user +
+
+ + + This text is only visible to an admin +
+
+ + ">Logout + + + \ No newline at end of file diff --git a/spring-security-mvc-session/src/main/webapp/WEB-INF/view/homepage.jsp b/spring-security-mvc-session/src/main/webapp/WEB-INF/view/homepage.jsp new file mode 100644 index 0000000000..afd2c6da59 --- /dev/null +++ b/spring-security-mvc-session/src/main/webapp/WEB-INF/view/homepage.jsp @@ -0,0 +1,22 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%> + + + + +

This is the homepage for the user

+ + + This text is only visible to a user +
+
+ + + This text is only visible to an admin +
+
+ + ">Logout + + + \ No newline at end of file diff --git a/spring-security-mvc-session/src/main/webapp/WEB-INF/view/login.jsp b/spring-security-mvc-session/src/main/webapp/WEB-INF/view/login.jsp new file mode 100644 index 0000000000..0eb857c62a --- /dev/null +++ b/spring-security-mvc-session/src/main/webapp/WEB-INF/view/login.jsp @@ -0,0 +1,30 @@ + + + + +

Login

+ +
+ + + + + + + + + + + + + + + + + +
User:
Password:
Remember Me:
+ +
+ + + \ No newline at end of file diff --git a/spring-security-mvc-session/src/main/webapp/WEB-INF/web.xml b/spring-security-mvc-session/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..bf691dc491 --- /dev/null +++ b/spring-security-mvc-session/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,57 @@ + + + + Spring MVC Custom Application + + + 1 + + + org.baeldung.web.SessionListenerWithMetrics + + + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + org.baeldung.spring + + + + org.springframework.web.context.ContextLoaderListener + + + + + mvc + org.springframework.web.servlet.DispatcherServlet + 1 + + + mvc + / + + + + + springSecurityFilterChain + org.springframework.web.filter.DelegatingFilterProxy + + + springSecurityFilterChain + /* + + + + + + + \ No newline at end of file diff --git a/spring-security-mvc-session/src/test/resources/.gitignore b/spring-security-mvc-session/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/spring-security-mvc-session/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file