springsecurity實(shí)現(xiàn)登錄驗(yàn)證以及根據(jù)用戶身份跳轉(zhuǎn)不同頁(yè)面
想關(guān)依賴,采用session加redis存儲(chǔ)用戶信息
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl-framework-starter</artifactId> <version>1.1.56.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api --> <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--session start--> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>5.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.0.8.RELEASE</version> </dependency> <!--end--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>mockwebserver</artifactId> <version>3.11.0</version> </dependency> <dependency><!--自動(dòng)生成getter,setter--> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.15</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.0.3</version> </dependency> <!--end-->
登錄處理類將用戶信息存入spring security(此類是通過username獲取用戶的合法用戶名,密碼,權(quán)限,并建立合法用戶,
spring security將自動(dòng)與用戶輸入的進(jìn)行匹配)
@Service @Transactional public class UserDetailsServiceIm implements UserDetailsService { private member memb,memRoles; @Autowired private MemberEn mem; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { System.out.println("Running:\tUserDetails loadUserByUsername"); memb=mem.getMember(username); if (memb == null){ throw new UsernameNotFoundException("user not found");//拋出異常,會(huì)根據(jù)配置跳到登錄失敗頁(yè)面 } memRoles=mem.getMemRole(memb.getId());//根據(jù)查詢的id查詢角色與urls User.UserBuilder builder; if(memb!=null) { String[] roles=memRoles.getRoleName().split(","); builder=org.springframework.security.core.userdetails.User.withUsername(username); builder.password(new BCryptPasswordEncoder().encode(memb.getPasswd())); for (String role:roles ) { System.out.println(role); } builder.roles(roles); } else { throw new UsernameNotFoundException("member not found"); } return builder.build(); }
webconfig類。
@Configuration @EnableWebSecurity public class WebSecConfig extends WebSecurityConfigurerAdapter { @Bean public UserDetailsService userDetailsService(){ return new UserDetailsServiceIm(); } @Bean public BCryptPasswordEncoder passwordEncoder(){//數(shù)據(jù)庫(kù)密碼密碼加密 return new BCryptPasswordEncoder(){ @Override public String encode(CharSequence rawPassword) { // return MD5Utiles.encode(String.valueOf(rawPassword)); return super.encode(rawPassword); } @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { // return encodedPassword.equals(MD5Utiles.encode(String.valueOf(rawPassword))); return super.matches(rawPassword, encodedPassword); } }; } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder()); } @Autowired MemberEn memberEn; @Override protected void configure(HttpSecurity http) throws Exception { Map<String,String[]> map=memberEn.getRoleUrl(); if (map!=null) { Iterator<?> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); String url = (String) entry.getKey(); String[] roles = (String[]) entry.getValue(); if (roles.length > 0) { http.authorizeRequests().antMatchers(url).hasAnyRole(roles).anyRequest().authenticated(); } } } else { System.out.println("未查詢到用戶類型對(duì)應(yīng)url的映射"); } http.authorizeRequests().anyRequest().authenticated() .and().formLogin().loginPage("/login").defaultSuccessUrl("/mem/index.html").failureUrl("/login/error").permitAll() .passwordParameter("password").usernameParameter("username") .and().logout().logoutUrl("/test/api/exit").logoutSuccessUrl("/login") .invalidateHttpSession(true).permitAll() .and().csrf().disable();//關(guān)閉CSRF保護(hù) } @Override//web security忽略以下url public void configure(WebSecurity web) throws Exception { super.configure(web); web.ignoring().antMatchers( "/**/*.js","/**/*.css","/**/*.js", "/**/*.jpg","/**/*.png","/**/*.jpeg","/test/*" ); }
編寫相應(yīng)的接口處理登錄狀態(tài)跳轉(zhuǎn)(登錄表單的action屬性設(shè)成/login,不然無(wú)法攔截登錄信息,這是spring security默認(rèn)的,也可以進(jìn)行修改)
@RequestMapping("/login")//內(nèi)部用戶登錄攔截,spring security登錄控制默認(rèn)攔截/login路徑,表單post為/login public ModelAndView login(){ return new ModelAndView("/login.html"); } @RequestMapping(value = "/login/error") public @ResponseBody String doLoginError(){ return "false"; }
控制層:攔截用戶請(qǐng)求,并根據(jù)用戶身份跳轉(zhuǎn)
@Controller @RequestMapping("/mem") public class MemEnContr { private ModelAndView modelAndView; @RequestMapping("/index.html")//根據(jù)角色跳轉(zhuǎn),這里對(duì)應(yīng)的是webconfig類中設(shè)置好的登錄成功url跳轉(zhuǎn) public ModelAndView doLogin(){ switch (PermissionServer.getAuthe()){ case "[ROLE_推薦單位]":modelAndView = new ModelAndView("redirect:/pro/hom/index.html"); break; case "[ROLE_計(jì)劃科]":modelAndView = new ModelAndView("redirect:/pro/hom/index.html"); break; case "[ROLE_admin]":modelAndView = new ModelAndView("redirect:/pro/hom/index.html"); break; case "[ROLE_其他科室]":modelAndView = new ModelAndView("redirect:/pro/hom/index.html"); break; case "[ROLE_受理中心]":modelAndView = new ModelAndView("redirect:/pro/hom/index.html"); default: modelAndView = new ModelAndView("redirect:/login");//未登錄或權(quán)限不夠 break; } return modelAndView; }
注意登錄表單post的action屬性要設(shè)置為與websecconfig類中一致才能被攔截,還需設(shè)置將驗(yàn)證成功的跳轉(zhuǎn)url指向控制層相應(yīng)的@RequestMapping
.and().formLogin().loginPage("/login").permitAll().defaultSuccessUr
用戶注銷登錄以及注銷后跳轉(zhuǎn)到登錄頁(yè)面:
.and().logout().logoutUrl("/mem/api/exit").logoutSuccessUrl("/login").permitAll().and().csrf().disable();//關(guān)閉CSRF保護(hù)
這里需要說明的是我們只需將頁(yè)面的注銷按鈕的src="./mem/api/exit"即可,不需要再控制層實(shí)現(xiàn)/mem/api/exit的相關(guān)方法,spring將自動(dòng)完成注銷操作
到此這篇關(guān)于springsecurity實(shí)現(xiàn)登錄驗(yàn)證以及根據(jù)用戶身份跳轉(zhuǎn)不同頁(yè)面的文章就介紹到這了,更多相關(guān)springsecurity 登錄驗(yàn)證及跳轉(zhuǎn)不同頁(yè)面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合SpringSecurity實(shí)現(xiàn)圖形驗(yàn)證碼功能
- SpringSecurity集成圖片驗(yàn)證碼的詳細(xì)過程
- SpringBoot?SpringSecurity?詳細(xì)介紹(基于內(nèi)存的驗(yàn)證)
- SpringSecurity添加圖形驗(yàn)證碼認(rèn)證實(shí)現(xiàn)
- SpringBoot+SpringSecurity+jwt實(shí)現(xiàn)驗(yàn)證
- Springboot+SpringSecurity實(shí)現(xiàn)圖片驗(yàn)證碼登錄的示例
- SpringSecurity從數(shù)據(jù)庫(kù)中獲取用戶信息進(jìn)行驗(yàn)證的案例詳解
- SpringSecurity實(shí)現(xiàn)圖形驗(yàn)證碼功能的實(shí)例代碼
- SpringBoot + SpringSecurity 短信驗(yàn)證碼登錄功能實(shí)現(xiàn)
- SpringSecurity實(shí)現(xiàn)多種身份驗(yàn)證方式
相關(guān)文章
解決nacos項(xiàng)目啟動(dòng)報(bào)錯(cuò):Connection refused: no further&
這篇文章主要介紹了解決nacos項(xiàng)目啟動(dòng)報(bào)錯(cuò):Connection refused: no further informa問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04java線程池不同場(chǎng)景下使用示例經(jīng)驗(yàn)總結(jié)
這篇文章主要為大家介紹了java線程池不同場(chǎng)景如何使用的示例源碼及經(jīng)驗(yàn)總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03SpringBoot配置多數(shù)據(jù)源的四種方式分享
在日常開發(fā)中我們都是以單個(gè)數(shù)據(jù)庫(kù)進(jìn)行開發(fā),在小型項(xiàng)目中是完全能夠滿足需求的,但是,當(dāng)我們牽扯到大型項(xiàng)目的時(shí)候,單個(gè)數(shù)據(jù)庫(kù)就難以承受用戶的CRUD操作,那么此時(shí),我們就需要使用多個(gè)數(shù)據(jù)源進(jìn)行讀寫分離的操作,本文就給大家介紹SpringBoot配置多數(shù)據(jù)源的方式2023-07-07如何使用@ConditionalOnExpression決定是否生效注釋
這篇文章主要介紹了如何使用@ConditionalOnExpression決定是否生效注釋的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Java使用Poi導(dǎo)出Excel表格方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Java使用Poi導(dǎo)出Excel表格的相關(guān)資料,Java POI是一個(gè)用于操作Microsoft Office格式的Java API庫(kù),可以使用它來(lái)導(dǎo)出Excel文件,需要的朋友可以參考下2023-10-10application.yml文件中如何開啟mybatis自動(dòng)駝峰映射
這篇文章主要介紹了application.yml文件中開啟mybatis自動(dòng)駝峰映射的方法,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08