這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Springboot2.X如何解決單點(diǎn)登陸,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的王屋網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
<dependency> <groupId>com.majiaxueyuan</groupId> <artifactId>sso-core</artifactId> <version>1.2.2</version> </dependency>
由于使用的是springboot2.2.0版本,所以配置需要實(shí)現(xiàn)WebMvcConfigurer
import com.majiaxueyuan.sso.core.filter.MaJiaSSOIntercepter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @Author:MuJiuTian * @Description: 單點(diǎn)登陸配置WebMvcConfigurer * @Date: Created in 下午5:34 2019/10/21 */ @Configuration public class SsoConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(ssoIntercepter()).addPathPatterns("/**"); } @Bean public MaJiaSSOIntercepter ssoIntercepter() { return new MaJiaSSOIntercepter().setTokenSalt("pwd_salt"); } }
如果是其他版本可以使用集成WebMvcConfigurerAdapter
import com.majiaxueyuan.sso.core.filter.MaJiaSSOIntercepter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * @Author:MuJiuTian * @Description: 單點(diǎn)登陸配置WebMvcConfigurerAdapter * @Date: Created in 下午5:34 2019/10/21 */ @Configuration public class SsoConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(ssoIntercepter()).addPathPatterns("/**"); } @Bean public MaJiaSSOIntercepter ssoIntercepter() { return new MaJiaSSOIntercepter().setTokenSalt("pwd_salt"); } }
@Data @AllArgsConstructor @NoArgsConstructor public class User { private int userId; private String slat; private String account; // ....其他的字段就不寫了 }
/** * @Author:MuJiuTian * @Description: 簡(jiǎn)單測(cè)試,我就不寫接口了,直接以class的形式 * @Date: Created in 下午5:47 2019/10/21 */ @Service public class LoginService { public User checkUser(String account, String pwd){ // 做假數(shù)據(jù),加入密碼登陸成功,返回user實(shí)體類 return new User(1,account,"ewdsbj"); } }
import com.majiaxueyuan.sso.core.annotation.NoToken; import com.majiaxueyuan.sso.core.constans.Result; import com.majiaxueyuan.sso.core.helper.TokenLoginHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @Author:MuJiuTian * @Description: 測(cè)試單點(diǎn)登錄框架 * @Date: Created in 下午5:25 2019/10/21 */ @RestController public class LoginController { @Autowired LoginService loginService; @GetMapping(value = "/testToken") public String testToken() { return "需要token"; } @GetMapping(value = "/login") @NoToken(notNeedToken = true) public String login(String account, String password) { // 測(cè)試用戶輸入的用戶名、密碼是否正確 User user = loginService.checkUser(account, password); if (user == null){ return "密碼錯(cuò)誤"; } // 如果密碼成功后,獲取用戶主鍵id、以及注冊(cè)時(shí)的salt Long user_id = Long.valueOf(user.getUserId()); String salt = user.getSlat(); // 然后通過(guò)sso-core框架獲取token Result loginSuccess = TokenLoginHelper.loginSuccess(user_id,account,"",salt); // 獲取token String token = ""; if (loginSuccess.getCode() == 200){ token = loginSuccess.getData().toString(); } return token; } }
測(cè)試接口1:
測(cè)試接口2:
用戶獲取到Token之后,返回給前端,前端攜帶時(shí)需要將Token添加到Header中的Authorization字段。當(dāng)前強(qiáng)制使用此請(qǐng)求頭字段,這些數(shù)據(jù)存儲(chǔ)headers中帶過(guò)來(lái)檢測(cè)token是否過(guò)期或者登陸...情況。
SSOUser user = (SSOUser) request.getAttribute("ssoUser");
當(dāng)有某個(gè)請(qǐng)求不需要認(rèn)證即可訪問(wèn)時(shí),在此請(qǐng)求方法上添加注解@NoToken,不加注解需要驗(yàn)證,只要加上注解就表示不需要認(rèn)證,不管@NoToken(notNeedToken = true)還是@NoToken(notNeedToken = false),都不需要驗(yàn)證,至少在目前最新1.2.2版本是這樣。
上述就是小編為大家分享的Springboot2.X如何解決單點(diǎn)登陸了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前題目:Springboot2.X如何解決單點(diǎn)登陸
文章來(lái)源:http://jinyejixie.com/article48/ppsphp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、商城網(wǎng)站、網(wǎng)站設(shè)計(jì)、微信小程序、網(wǎng)站設(shè)計(jì)公司、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)