【Jsp精品源码栏目提醒】:网学会员,鉴于大家对Jsp精品源码十分关注,论文会员在此为大家搜集整理了“【精品】spring Security 3.1的配置 - 其它资料”一文,供大家参考学习!
spring Security 3.1 的配置一.spring Security 3.1 最基本的配置1. lib 里的 jar 包,就用 3.1 中的示例内的包即可。
2. web.xmlltxml versionquot1.0quot encodingquotUTF-8quotgtltweb-app xmlns:xsiquothttp://www.w3.org/2001/XMLSchema-instancequotxmlnsquothttp://java.sun.com/xml/ns/javaeequotxmlns:webquothttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsdquotxsi:schemaLocationquothttp://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsdquot versionquot2.5quotgt lt-- 加载spring配置文件 classpath 为扫描classpath目录下,applicationContext-.xml为以applicationContext-开头的所有xml文件 --gt ltcontext-paramgt ltparam-namegtcontextConfigLocationlt/param-namegt ltparam-valuegt classpath:applicationContext-.xml lt/param-valuegt lt/context-paramgt lt-- 加载spring Security 过滤器 --gt ltfiltergt ltfilter-namegtspringSecurityFilterChainlt/filter-namegtltfilter-classgtorg.springframework.web.filter.DelegatingFilterProxylt/filter-classgt lt/filtergt lt-- 拦截所有请求。
--gt ltfilter-mappinggt ltfilter-namegtspringSecurityFilterChainlt/filter-namegt lturl-patterngt/lt/url-patterngt lt/filter-mappinggt lt-- 为spring添加监听器 --gt ltlistenergtltlistener-classgtorg.springframework.web.context.ContextLoaderListenerlt/listener-classgt lt/listenergt ltwelcome-file-listgt ltwelcome-filegtindex.jsplt/welcome-filegt lt/welcome-file-listgtlt/web-appgt3. applicationContext-security.xmlltxml versionquot1.0quot encodingquotUTF-8quotgtltbeans:beans xmlnsquothttp://www.springframework.org/schema/securityquot xmlns:beansquothttp://www.springframework.org/schema/beansquot xmlns:xsiquothttp://www.w3.org/2001/XMLSchema-instancequot xsi:schemaLocationquothttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.1.xsdquotgtlt-- 自动配置模式,拦截所有请求,有ROLE_USER才可以通过 --gt lthttp auto-configquottruequotgt ltintercept-url patternquot/quot accessquotROLE_USERquot /gt lt/httpgt lt-- 认证管理器。
用户名密码都集成在配置文件中 --gt ltauthentication-managergt ltauthentication-providergt ltuser-servicegt ltuser namequotuserquot passwordquotuserquot authoritiesquotROLE_USERquot/gt lt/user-servicegt lt/authentication-providergt lt/authentication-managergtlt/beans:beansgt4.发布,测试。
当打开首页时,因权限不足会自动跳转到默认登录页面。
登录成功后,便可访问默认首页。
二.自定义登录页。
1. 只需要修改一个文件applicationContext-security.xmlltxml versionquot1.0quot encodingquotUTF-8quotgtltbeans:beans xmlnsquothttp://www.springframework.org/schema/securityquot xmlns:beansquothttp://www.springframework.org/schema/beansquot xmlns:xsiquothttp://www.w3.org/2001/XMLSchema-instancequot xsi:schemaLocationquothttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.1.xsdquotgt lt-- 开启测试模式 --gt ltdebug /gtlt-- 自动配置模式,拦截所有请求进行匹配,有ROLE_USER才可以通过 --gt lthttp auto-configquottruequotgt lt-- 指定不拦截登录页, 表示可以带参数。
从3.10开始,不再支持filtersquotnonequot配置 --gt ltintercept-url patternquot/login.jspquotaccessquotIS_AUTHENTICATED_ANONYMOUSLYquot /gt ltintercept-url patternquot/quot accessquotROLE_USERquot /gt lt-- 指定登录页面及登录失败跳转页 --gt ltform-login login-pagequot/login.jspquotauthentication-failure-urlquot/login.jsplogin_error1quot/gt lt/httpgt lt-- 认证管理器 --gt ltauthentication-managergt ltauthentication-providergt ltuser-servicegt ltuser namequotuserquot passwordquotuserquot authoritiesquotROLE_USERquot/gt lt/user-servicegt lt/authentication-providergt lt/authentication-managergt lt-- 指定中文资源 。
默认命名空间是security所以要加前缀beans: --gt ltbeans:bean idquotmessageSourcequot classquotorg.springframework.context.support.ReloadableResourceBundleMessageSourcequotgt ltbeans:property namequotbasenamequotvaluequotclasspath:org/springframework/security/messages_zh_CNquot/gt lt/beans:beangtlt/beans:beansgt2.增加一个登录页。
可以从示例代码中拷贝过来。
以下为对源代码做了简单的修改login.jsplttaglib prefixquotcquot uriquothttp://java.sun.com/
jsp/jstl/corequot gtlt page pageEncodingquotUTF-8quot gtlthtmlgt ltheadgt lttitlegt登录页lt/titlegt lt/headgt ltbody onloadquotdocument.f.j_username.focusquotgt lth1gt请登录lt/h1gt ltpgt地区代码: lt request.getLocale gtlt/pgt lt-- this form-login-page form is also used as the form-error-page to ask for a login again. --gt ltc:if testquotnot empty param.login_errorquotgt ltfont colorquotredquotgt 登录失败,请重试.ltbr/gtltbr/gt 原因: ltc:outvaluequotSPRING_SECURITY_LAST_EXCEPTION.messagequot/gt. lt/fontgt lt/c:ifgt ltform namequotfquot actionquotltc:url valuej_spring_security_check/gtquotmethodquotPOSTquotgt lttablegt lttrgtlttdgt用户名:lt/tdgtlttdgtltinput typetext namej_usernamevalueltc:if testquotnot empty param.login_errorquotgtltc:outvaluequotSPRING_SECURITY_LAST_USERNAMEquot/gtlt/c:ifgt/gtlt/tdgtlt/trgt lttrgtlttdgt密 码:lt/tdgtlttdgtltinput typepasswordnamej_passwordgtlt/tdgtlt/trgt lttrgtlttdgtltinput typequotcheckboxquotnamequot_spring_security_remember_mequotgtlt/tdgtlttdgtDont ask for my passwordfor two weekslt/tdgtlt/trgt lttrgtlttd colspan2gtltinput namequotsubmitquottypequotsubmitquotgtlt/tdgtlt/trgt lttrgtlttd colspan2gtltinput namequotresetquot typequotresetquotgtlt/tdgtlt/trgt lt/tablegt lt/formgt lt/bodygtlt/htmlgt3.重新发布,测试。
当访问主页时,便跳转到 login.
jsp 页面上了。
填写用户名:user。
密码:user,便可成功登录,访问主页。
验证失败还有个简单提示。
三.密码加密 1. 只需要修改一个文件applicationContext-security.xmlltxml versionquot1.0quot encodingquotUTF-8quotgtltbeans:beans xmlnsquothttp://www.springframework.org/schema/securityquot xmlns:beansquothttp://www.springframework.org/schema/beansquot xmlns:xsiquothttp://www.w3.org/2001/XMLSchema-instancequot xsi:schemaLocationquothttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.1.xsdquotgt lt-- 开启测试模式 --gt ltdebug /gtlt-- 自动配置模式,拦截所有请求进行匹配,有ROLE_USER才可以通过 --gt lthttp auto-configquottruequotgt lt-- 指定不拦截登录页, 表示可以带参数。
从3.10开始,不再支持filtersquotnonequot配置 --gt ltintercept-url patternquot/login.jspquotaccessquotIS_AUTHENTICATED_ANONYMOUSLYquot /gt ltintercept-url patternquot/quot accessquotROLE_USERquot /gt lt-- 指定登录页面及登录失败跳转页 --gt ltform-login login-pagequot/login.jspquotauthentication-failure-urlquot/login.jsplogin_error1quot/gt lt/httpgt lt-- 认证管理器 --gt ltauthentication-managergt ltauthentication-providergt lt-- 启用密码加密码,方式为md5 ee11cbb19052e40b07aac0ca060c23ee为user的MD5加密 --gt ltpassword-encoder hashquotmd5quot/gt ltuser-servicegt ltuser namequotuserquotpasswordquotee11cbb19052e40b07aac0ca060c23eequot authoritiesquotROLE_USERquot/gt lt/user-servicegt lt/authentication-providergt lt/authentication-managergt lt-- 指定中文资源 --gt ltbeans:bean idquotmessageSourcequot classquotorg.springframework.context.support.ReloadableResourceBundleMessageSourcequotgt ltbeans:property namequotbasenamequotvaluequotclasspath:org/springframework/security/messages_zh_CNquot/gt lt/beans:beangtlt/beans:beansgt 。
。
2.重新发布。
。
。
四.将用户角色及权限放入数据库(mysql 5)。
首先先配置数据源,可以是任何的库。
Jar 包就用下载的 ss3.1 里的包就可以。
以下以 mysql 为例。
1.定义三个表:用 u_user,权限表 u_authority,用户权限表 u_role。
CREATE DATABASE initlifeCHARACTER SET utf8COLLATE utf8_general_ciCREATE TABLE u_user us_name VARCHAR50 NOT NULLus_password VARCHAR50 NOT NULLus_enabled TINYINT1 NULL DEFAULT NULLPRIMARY KEY us_nameUNIQUE INDEX u_name us_nameCOLLATEutf8_general_ciENGINEInnoDBCREATE TABLE u_authority au_authority VARCHAR50 NOT NULLau_name VARCHAR50 NULL DEFAULT NULLPRIMARY KEY au_authorityUNIQUE INDEX au_authority au_authorityCOLLATEutf8_general_ciENGINEInnoDBCREATE TABLE u_role ro_usname VARCHAR50 NOT NULLro_auauthority VARCHAR50 NULL DEFAULT NULLINDEX FK_u_role_u_user ro_usnameINDEX FK_u_role_u_authority ro_auauthorityCONSTRAINT FK_u_role_u_authority FOREIGN KEY ro_auauthorityREFERENCES u_authority au_authority ON UPDATE CASCADE ON DELETECASCADECONSTRAINT FK_u_role_u_user FOREIGN KEY ro_usname REFERENCESu_user us_name ON UPDATE CASCADE ON DELETE CASCADECOLLATEutf8_general_ciENGINEInnoDB2.添加数据INSERT INTO u_authority au_authority au_name VALUESROLE_ADMIN 管理员ROLE_GUESS 访客ROLE_USER 普通用户INSERT INTO u_role ro_usname ro_auauthority VALUESuser ROLE_USERuser ROLE_GUESSadmin ROLE_ADMINadmin ROLE_USERguest ROLE_GUESSINSERT INTO u_user us_name us_password us_enabled VALUESadmin 21232f297a57a5a743894a0e4a801fc3 1guest 084e0343a0486ff05530df6c705c8bb4 0 user ee11cbb19052e40b07aac0ca060c23ee 13.修改applicationContext-security.xmlltxml versionquot1.0quot encodingquotUTF-8quotgtltbeans:beans xmlnsquothttp://www.springframework.org/schema/securityquot xmlns:beansquothttp://www.springframework.org/schema/beansquot xmlns:xsiquothttp://www.w3.org/2001/XMLSchema-instancequot xsi:schemaLocationquothttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.1.xsdquotgt lt-- 开启测试模式 --gt lt-- 自动配置模式,拦截所有请求进行匹配,有ROLE_USER才可以通过 --gt lthttp auto-configquottruequotgt lt-- 指定不拦截登录页, 表示可以带参数。
从3.10开始,不再支持filtersquotnonequot配置 --gt ltintercept-url patternquot/login.jspquotaccessquotIS_AUTHENTICATED_ANONYMOUSLYquot /gt ltintercept-url patternquot/quot accessquotROLE_USERquot /gt lt-- 指定登录页面及登录失败跳转页 --gt ltform-login login-pagequot/login.jspquotauthentication-failure-urlquot/login.jsplogin_error1quot/gt lt/httpgt lt-- 认证管理器 --gt ltauthentication-managergt ltauthentication-providergt ltpassword-encoder hashquotmd5quot/gt ltjdbc-user-service data-source-refquotdataSourcequot users-by-username-queryquotselect u.us_nameusername u.us_password password u.us_enabled enabled from u_user uwhere u.us_name and u.us_enabled 1quot authorities-by-username-queryquotselectr.ro_usnamer.ro_auauthority from u_role r where r.ro_usname quot /gt lt/authentication-providergt lt/authentication-managergt lt-- 指定中文资源 --gt ltbeans:bean idquotmessageSourcequot classquotorg.springframework.context.support.ReloadableResourceBundleMessageSourcequotgt ltbeans:property namequotbasenamequotvaluequotclasspath:org/springframework/security/messages_zh_CNquot/gt lt/beans:beangtlt/beans:beansgt4.我的 web.xmlltxml versionquot1.0quot encodingquotUTF-8quotgtltweb-app versionquot2.5quot xmlnsquothttp://java.sun.com/xml/ns/javaeequot xmlns:xsiquothttp://www.w3.org/2001/XMLSchema-instancequot xsi:schemaLocationquothttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsdquotgt lt-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --gt ltcontext-paramgt ltparam-namegtcontextConfigLocationlt/param-namegt ltparam-valuegtclasspath:applicationContext-.xmllt/param-valuegt lt/context-paramgt lt-- 对Spring容器进行实例化 --gt ltlistenergtltlistener-classgtorg.springframework.web.context.ContextLoaderListenerlt/listener-classgt lt/listenergt lt-- 加载spring Security 过滤器 --gt ltfiltergt ltfilter-namegtspringSecurityFilterChainlt/filter-namegtltfilter-classgtorg.springframework.web.filter.DelegatingFilterProxylt/filter-classgt lt/filtergt lt-- 拦截所有请求。
--gt ltfilter-mappinggt ltfilter-namegtspringSecurityFilterChainlt/filter-namegt lturl-patterngt/lt/url-patterngt lt/filter-mappinggt ltwelcome-file-listgt ltwelcome-filegtindex.jsplt/welcome-filegt lt/welcome-file-listgtlt/web-appgt5.我的另外配置文件applicationContext-common.xmlltxml versionquot1.0quot encodingquotUTF-8quotgtltbeans xmlnsquothttp://www.springframework.org/schema/beansquot xmlns:xsiquothttp://www.w3.org/2001/XMLSchema-instancequot xmlns:pquothttp://www.springframework.org/schema/pquot xsi:schemaLocationquothttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdquotgtltbean idquotdataSourcequotclassquotcom.mchange.v2.c3p0.ComboPooledDataSourcequotdestroy-methodquotclosequotgt ltproperty namequotdriverClassquot valuequotorg.gjt.mm.mysql.Driverquot/gt ltproperty namequotjdbcUrlquotvaluequotjdbc:mysql://localhost:3306/sstuseUnicodetrueampcharacterEncodingUTF-8quot/gt ltproperty namequotuserquot valuequotrootquot/gt ltproperty namequotpasswordquot valuequot123456quot/gt lt--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。
Default: 3 --gt ltproperty namequotinitialPoolSizequot valuequot1quot/gt lt--连接池中保留的最小连接数。
--gt ltproperty namequotminPoolSizequot valuequot1quot/gt lt--连接池中保留的最大连接数。
Default: 15 --gt ltproperty namequotmaxPoolSizequot valuequot300quot/gt lt--最大空闲时间60秒内未使用则连接被丢弃。
若为0则永不丢弃。
Default: 0--gt ltproperty namequotmaxIdleTimequot valuequot60quot/gt lt--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。
Default: 3 --gt ltproperty namequotacquireIncrementquot valuequot5quot/gt lt--每60秒检查所有连接池中的空闲连接。
Default: 0 --gt ltproperty namequotidleConnectionTestPeriodquot valuequot60quot/gt lt/beangt ltbean idquotsessionFactoryquotclassquotorg.springframework.orm.hibernate3.LocalSessionFactoryBeanquotgt ltproperty namequothibernatePropertiesquotgt ltvaluegt hibernate.dialectorg.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.autoupdate hibernate.show_sqlfalse hibernate.format_sqlfalse lt/valuegt lt/propertygt lt/beangt ltbean idquottxManagerquotclassquotorg.springframework.orm.hibernate3.HibernateTransactionManagerquotgt ltproperty namequotsessionFactoryquot refquotsessionFactoryquot/gt lt/beangt lt/beansgt6.可以发布测试了。
五.开启页面 ss EL 表达式1.修改 plicationContext-security.xmlltxml versionquot1.0quot encodingquotUTF-8quotgtltbeans:beans xmlnsquothttp://www.springframework.org/schema/securityquot xmlns:beansquothttp://www.springframework.org/schema/beansquot xmlns:xsiquothttp://www.w3.org/2001/XMLSchema-instancequot xsi:schemaLocationquothttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.1.xsdquotgt lt-- 开启测试模式 --gtlt-- 自动配置模式,拦截所有请求进行匹配,有ROLE_USER才可以通过 --gt lthttp auto-configquottruequot use-expressionsquottruequotaccess-denied-pagequot/403.jspquot gt lt-- 指定不拦截登录页, 表示可以带参数。
从3.10开始,不再支持filtersquotnonequot配置 --gt ltintercept-url patternquot/login.jspquot accessquotpermitAllquot /gt ltintercept-url patternquot/user.jspquot accessquothasRoleROLE_USERquot/gt ltintercept-url patternquot/admin.jspquotaccessquothasRoleROLE_ADMINquot/gt ltintercept-url patternquot/quot accessquothasRoleROLE_USERquot/gt lt-- 指定登录页面及登录失败跳转页 --gt ltform-login login-pagequot/login.jspquotauthentication-failure-urlquot/login.jsplogin_error1quot/gt lt/httpgt lt-- 认证管理器 --gt ltauthentication-managergt ltauthentication-providergt ltpassword-encoder hashquotmd5quot/gt ltjdbc-user-service data-source-refquotdataSourcequot users-by-username-queryquotselect u.us_nameusername u.us_password password u.us_enabled enabled from u_user uwhere u.us_name .
上一篇:
双向转诊系统【毕业论文,绝对精品】
下一篇:
爱情只不过就是,爱与不爱