javascript
Spring MVC定制用户登录注销实现示例
該代碼示例可從Spring-MVC-Login-Logout目錄中的Github獲得。 它從帶有注釋示例的Spring MVC派生而來(lái)。
定制身份驗(yàn)證提供者
為了實(shí)現(xiàn)我們自己的接受用戶登錄請(qǐng)求的方式,我們需要實(shí)現(xiàn)身份驗(yàn)證提供程序。 如果用戶的ID與密碼相同,以下內(nèi)容可讓用戶進(jìn)入:
public class MyAuthenticationProvider implements AuthenticationProvider {private static final List<GrantedAuthority> AUTHORITIES= new ArrayList<GrantedAuthority>();static {AUTHORITIES.add(new SimpleGrantedAuthority('ROLE_USER'));AUTHORITIES.add(new SimpleGrantedAuthority('ROLE_ANONYMOUS'));}@Overridepublic Authentication authenticate(Authentication auth)throws AuthenticationException {if (auth.getName().equals(auth.getCredentials())) {return new UsernamePasswordAuthenticationToken(auth.getName(),auth.getCredentials(), AUTHORITIES);}throw new BadCredentialsException('Bad Credentials');}@Overridepublic boolean supports(Class<?> authentication) {if ( authentication == null ) return false;return Authentication.class.isAssignableFrom(authentication);}}
Security.xml
我們需要?jiǎng)?chuàng)建一個(gè)security.xml文件:
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.1.xsd'><http><intercept-url pattern='/*' access='ROLE_ANONYMOUS'/><form-logindefault-target-url='/'always-use-default-target='true' /><anonymous /><logout /></http><authentication-manager alias='authenticationManager'><authentication-provider ref='myAuthenticationProvider' /></authentication-manager><beans:bean id='myAuthenticationProvider'class='com.jverstry.LoginLogout.Authentication.MyAuthenticationProvider' /></beans:beans>以上內(nèi)容可確保所有用戶都具有匿名角色來(lái)訪問(wèn)任何頁(yè)面。 登錄后,它們將重定向到主頁(yè)。 如果他們沒(méi)有登錄,他們將被自動(dòng)視為匿名用戶。 還聲明了注銷功能。 與其重新實(shí)現(xiàn)輪子,不如使用Spring本身提供的項(xiàng)目。
主頁(yè)
我們實(shí)現(xiàn)了一個(gè)主頁(yè),顯示當(dāng)前登錄用戶的名稱以及登錄和注銷鏈接:
<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %> <!doctype html> <html lang='en'> <head><meta charset='utf-8'><title>Welcome To MVC Customized Login Logout!!!</title> </head><body><h1>Spring MVC Customized Login Logout !!!</h1>Who is currently logged in? <c:out value='${CurrPrincipal}' /> !<br /><a href='<c:url value='/spring_security_login'/>'>Login</a>?<a href='<c:url value='/j_spring_security_logout'/>'>Logout</a></body> </html>
控制者
我們需要向視圖提供當(dāng)前登錄的用戶名:
@Controller public class MyController {@RequestMapping(value = '/')public String home(Model model) {model.addAttribute('CurrPrincipal',SecurityContextHolder.getContext().getAuthentication().getName());return 'index';}}
運(yùn)行示例
編譯后,可以通過(guò)瀏覽以下示例開(kāi)始示例:http:// localhost:9292 / spring-mvc-login-logout /。 它將顯示以下內(nèi)容:
使用相同的ID和密碼登錄:
該應(yīng)用程序返回主窗口并顯示:
更多春天相關(guān)的帖子在這里 。
祝您編程愉快,別忘了分享!
參考: Spring MVC定制的用戶登錄注銷實(shí)現(xiàn)示例,來(lái)自我們的JCG合作伙伴 Jerome Versrynge,在技術(shù)說(shuō)明博客中。
翻譯自: https://www.javacodegeeks.com/2012/10/spring-mvc-customized-user-login-logout.html
總結(jié)
以上是生活随笔為你收集整理的Spring MVC定制用户登录注销实现示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 动作游戏《行尸走肉:背叛》9 月 15
- 下一篇: Spring:设置日志依赖项