SpringBoot結(jié)合JWT登錄權(quán)限控制的實(shí)現(xiàn)
最近項(xiàng)目中使用springboot+jwt實(shí)現(xiàn)登錄權(quán)限控制,所以在這里記錄一下防止以后忘記,畢竟好記性不如爛筆頭嘛~。
首先我們需要導(dǎo)入使用到的jwt的包:
<dependency> ? ? <groupId>io.jsonwebtoken</groupId> ? ? <artifactId>jjwt</artifactId> ? ? <version>0.8.0</version> </dependency> <dependency> ? ? <groupId>com.auth0</groupId> ? ? <artifactId>java-jwt</artifactId> ? ? <version>3.2.0</version> </dependency>
一、準(zhǔn)備LoginUser(存放登錄用戶信息) 和JwtUser
LoginUser.java
public class LoginUser {
? ? private Integer userId;
? ? private String username;
? ? private String password;
? ? private String role;
? ? 生成getter和setter......
}JwtUser.java
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
import java.util.Collections;
public class JwtUser implements UserDetails{
? ? private Integer id;
? ? private String username;
? ? private String password;
? ? private Collection<? extends GrantedAuthority> authorities;
? ? public JwtUser(){
? ? }
? ? public JwtUser(LoginUser loginUser){
? ? ? ? this.id = loginUser.getUserId();
? ? ? ? this.username = loginUser.getUsername();
? ? ? ? this.password = loginUser.getPassword();
? ? ? ? authorities = Collections.signleton(new SimpleGrantedAuthority(loginUser.getRole()));
? ? }
? ? @Override
? ? public Collection<? extends GrantedAuthority> getAuthorities(){
? ? ? ? return authorities;
? ? }
? ? @Override
? ? public String getPassword(){
? ? ? ? return password;
? ? }
? ? @Override
? ? public String getUsername(){
? ? ? ? return username;
? ? }
? ? //賬號(hào)是否未過(guò)期
? ? @Override
? ? public boolean isAccountNonExpired(){
? ? ? ? return true;
? ? }
? ? //賬號(hào)是否未鎖定
? ? @Override
? ? public boolean isAccountNonLocked(){
? ? ? ? return true
? ? }
? ? //賬號(hào)憑證是否未過(guò)期
? ? @Override
? ? public boolean isCredentialsNonExpired(){
? ? ? ? return true;
? ? }
? ? @Override
? ? public boolean isEnabled(){
? ? ? ? return true;
? ? }
? ??
}二、準(zhǔn)備JwtTokenUtils
import com.bean.JwtUser;
import io.jsonwebtoken.*;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class JwtTokenUtils {
? ??
? ? public static final String TOKEN_HEADER = "Authorization";
? ? public static final String TOKEN_PREFIX = "Bearer ";
? ? public static final String SECRET = "jwtsecret";
? ? public static final String ISS = "echisan";
? ? private static final Long EXPIRATION = 60 * 60 * 3;//過(guò)期時(shí)間3小時(shí)
? ? private static final String ROLE = "role";
? ? //創(chuàng)建token
? ? public static String createToken(String username, String role, boolean isRememberMe){
? ? ? ? Map map = new HashMap();
? ? ? ? map.put(ROLE, role);
? ? ? ? return Jwts.builder()
? ? ? ? ? ? ? ? ?.signWith(SignatureAlgorithm.HS512, SECRET)
? ? ? ? ? ? ? ? ?.setClaims(map)
? ? ? ? ? ? ? ? ?.setIssuer(ISS)
? ? ? ? ? ? ? ? ?.setSubject(username)
? ? ? ? ? ? ? ? ?.setIssuedAt(new Date())
? ? ? ? ? ? ? ? ?.setExpiration(new Date(System.currentTimeMillis() + expiration * 1000))
? ? ? ? ? ? ? ? ?.compact();
? ? }
? ? //從token中獲取用戶名(此處的token是指去掉前綴之后的)
? ? public static String getUserName(String token){
? ? ? ? String username;
? ? ? ? try {
? ? ? ? ? ? username = getTokenBody(token).getSubject();
? ? ? ? } catch ( ? ?Exception e){
? ? ? ? ? ? username = null;
? ? ? ? }
? ? ? ? return username;
? ? }
? ? public static String getUserRole(String token){
? ? ? ? return (String) getTokenBody(token).get(ROLE);
? ? }
? ? private static Claims getTokenBody(String token){
? ? ? ? Claims claims = null;
? ? ? ? try{
? ? ? ? ? ? claims = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
? ? ? ? } catch(ExpiredJwtException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch(UnsupportedJwtException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch(MalformedJwtException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch(SignatureException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } catch(IllegalArgumentException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? //是否已過(guò)期
? ? public static boolean isExpiration(String token){
? ? ? ? try{
? ? ? ? ? ? return getTokenBody(token).getExpiration().before(new Date());
? ? ? ? } catch(Exception e){
? ? ? ? ? ? e.printStackTrace;
? ? ? ? }
? ? ? ? return true;
? ? }
}三、準(zhǔn)備JWTAuthenticationFilter (驗(yàn)證登錄)、JWTAuthorizationFilter (鑒定權(quán)限)和UserDetailsServiceImpl類(lèi) (查庫(kù)匹配賬號(hào)密碼)
1.JWTAuthenticationFilter.java (驗(yàn)證登錄)
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter{
? ? private AuthenticationManager authenticationManager;
? ? public JWTAuthenticationFilter(AuthenticationManager authenticationManager){
? ? ? ? this.authenticationManager = authenticationManager;
? ? ? ? setAuthenticationFailureHandler(new FailHandler());//設(shè)置賬號(hào)密碼錯(cuò)誤時(shí)的處理方式
? ? }
? ? @Override
? ? public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException{
? ? ? ? //從輸入流中獲取登錄的信息
? ? ? ? String username = request.getParameter("username");
? ? ? ? String password = request.getParameter("password");
? ? ? ? return authenticationManager.authenticate(
? ? ? ? ? ? ? ? ? ? ? ?new UsernamePasswordAuthenticationToken(username, password, new ArrayList<>())
? ? ? ? );
? ? }
? ? @Override
? ? protected void successfulAuthentication(HttpServletRequest request,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpServletResponse response, FilterChain chain, Authentication authResult
) throws IOException, ServletException{
? ? ? ? JwtUser jwtUser = (JwtUser) authResult.getPrincipal();
? ? ? ? Collection<? extends GrantedAuthority> authorities = jwtUser.getAuthorities();
? ? ? ? String role = "";
? ? ? ? for(GrantedAuthority authority : authorities){
? ? ? ? ? ? role = authority.getAuthority();
? ? ? ? }
? ? ? ? String token = JwtTokenUtils.createToken(jwtUser.getUsername, role, false);
? ? ? ? //返回創(chuàng)建成功的token
? ? ? ? //但是這里創(chuàng)建的token只是單純的token,按照jwt的規(guī)定,
? ? ? ? //最后請(qǐng)求的格式應(yīng)該是 “Bearer token“。
? ? ? ? response.addHeader(JwtTokenUtils.TOKEN_HEADER, JwtTokenUtils.TOKEN_PREFIX + token);
? ? }
? ? //@Override
? ? //protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
? ? // ? ? ? ? ? ? ? ? ? ? ? ? ?AuthenticationException failed) throws IOException, ServletException {
? ? // ? ?response.getWriter().write("authentication failed, reason: " + failed.getMessage());
? ? //}
}2.JWTAuthorizationFilter.java (鑒定權(quán)限)
public class JWTAuthorizationFilter extends BasicAuthenticationFilter{
? ? public JWTAuthorizationFilter(AuthenticationManager authenticationManager){
? ? ? ? super(authenticationManager);
? ? }
? ? @Override
? ? protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? FilterChain chain) throws IOException, ServletException {
? ? ? ? String tokenHeader = request.getHeader(JwtTokenUtils.TOKEN_HEADER);
? ? ? ? //如果請(qǐng)求頭中沒(méi)有Authorization信息則直接放行了
? ? ? ? if(tokenHeader == null || !tokenHeader.startsWith(JwtTokenUtils.TOKEN_PREFIX)){
? ? ? ? ? ? chain.doFilter(request, response);
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? //如果請(qǐng)求頭中有token,則進(jìn)行解析,并且設(shè)置認(rèn)證信息
? ? ? ? if(!JwtTokenUtils.isExpiration(tokenHeader.replace(JwtTokenUtils.TOKEN_PREFIX, “”))){
? ? ? ? ? ? SecurityContextHolder.getContext().setAuthentication(getAuthentication(tokenHeader));
? ? ? ? }
? ? ? ? chain.doFilter(request, response);
? ? }
? ? private UsernamePasswordAuthenticationToken getAuthentication(String tokenHeader){
? ? ? ? String token = tokenHeader.replace(JwtTokenUtils.TOKEN_PREFIX, “”);
? ? ? ? String username = JwtTokenUtils.getUserName(token);
? ? ? ? if(username != null){
? ? ? ? ? ? return new UsernamePasswordAuthenticationToken(username, null, new ArrayList<>());
? ? ? ? ? ??
? ? ? ? }
? ? ? ? return null;
? ? }
}3.UserDetailsServiceImpl.java (查庫(kù)匹配賬號(hào)密碼)
import org.springframework.security.core.userdetails.UserDetailsService;
@Service
public class UserDetailsServiceImpl implements UserDetailsService{
? ? @Autowired
? ? UserMapper userMapper;
? ??
? ? public BCryptPasswordEncoder bCryptPasswordEncoder(){
? ? ? ? return new BCryptPasswordEncoder();
? ? }
? ? @Override
? ? public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
? ? ? ? LoginUser loginUser = usersMapper.selectByUserAccount(s);
? ? ? ? loginUser.setPassword(bCryptPasswordEncoder().encode(loginUser.getPassword()));
? ? ? ? return new JwtUser(loginUser);
? ? }
}四、FailHandler(賬號(hào)密碼錯(cuò)誤時(shí)的處理方式)
public class FailHandler extends SimpleUrlAuthenticationFailureHandler {
? ? @Override
? ? public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?AuthenticationException exception) throws IOException, ServletException {
? ? ? ? String errorMsg = exception.getMessage();
? ? ? ? if(exception instanceof BadCredentialsException){
? ? ? ? ? ? errorMsg = "用戶名或密碼錯(cuò)誤";
? ? ? ? }
? ? ? ? response.setHeader("content-type", "application/json");
? ? ? ? response.getOutputStream().write(JSONObject.fromObject(new AjaxResult(errorMsg, false)).toString().getBytes("utf-8"));
? ? }
}五、配置SecurityConfig
這個(gè)類(lèi)里規(guī)定了權(quán)限的相關(guān)信息
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{
? ??
? ? @Autowired
? ? private UserDetailsService userDetailsService;
? ? @Bean
? ? public BCryptPasswordEncoder bCryptPasswordEncoder(){
? ? ? ? return new BCryptPasswordEncoder();
? ? }
? ? @Override
? ? protected void configure(AuthenticationManagerBuilder auth) throws Exception{
? ? ? ? auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
? ? }
? ? @Override
? ? public void configure(WebSecurity web) throws Exception{
? ? ? ? web.ignoring().antMatchers("/static/**");
? ? }
? ? @Override
? ? protected void configure(HttpSecurity http) throws Exception{
? ? ? ? http.cors().and().csrf().disable()
? ? ? ? ? ? ? ? ? .authorizeRequests()
? ? ? ? ? ? ? ? ? .antMatchers(HttpMethod.POST, "/login").permitAll()//都可以訪問(wèn)
? ? ? ? ? ? ? ? ? .antMatchers("/").permitAll()
? ? ? ? ? ? ? ? ? .antMatchers("/index.html").permitAll()
? ? ? ? ? ? ? ? ? .antMatchers("/*.js").permitAll()
? ? ? ? ? ? ? ? ? .antMatchers("/*.css").permitAll()
? ? ? ? ? ? ? ? ? .antMatchers("/*.png").permitAll()
? ? ? ? ? ? ? ? ? .antMatchers("/*.svg").permitAll()
? ? ? ? ? ? ? ? ? .antMatchers("/*.woff").permitAll()
? ? ? ? ? ? ? ? ? .antMatchers("/*.ttf").permitAll()
? ? ? ? ? ? ? ? ? .antMatchers("/*.eot").permitAll()
? ? ? ? ? ? ? ? ? .antMatchers("/test/*").permitAll()//對(duì)接口的權(quán)限控制,表示對(duì)于"/test"路徑下的所有接口無(wú)需登錄也可以訪問(wèn)
? ? ? ? ? ? ? ? ? .antMatchers("/swagger-ui.html", "/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security",
? ? ? ? ? ? ? ? ? ? ? ? ? ?"/webjars/**", "/swagger-resources/configuration/ui").permitAll()//表示對(duì)swagger頁(yè)面放行
? ? ? ? ? ? ? ? ? .anyRequest().authenticated()//表示其余所有請(qǐng)求需要登陸之后才能訪問(wèn)
? ? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? ? .addFilter(new JWTAuthenticationFilter(authenticationManager()))
? ? ? ? ? ? ? ? ? .addFilter(new JWTAuthorizationFilter(authenticationManager()))
? ? ? ? ? ? ? ? ? .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
? ? }
? ? @Bean
? ? CorsConfigurationSource corsConfigurationSource() {
? ? ? ? final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
? ? ? ? source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
? ? ? ? return source;
? ? }
}到此springboot+jwt實(shí)現(xiàn)登錄驗(yàn)證功能就已實(shí)現(xiàn),其實(shí)在securityConfig中還可以加入對(duì)某個(gè)接口的角色權(quán)限,因?yàn)轫?xiàng)目中沒(méi)有用到所以這里并沒(méi)有加上,其實(shí)也很簡(jiǎn)單,只是把permitAll()換成其他方法,想實(shí)現(xiàn)的話請(qǐng)自行百度啦。更多相關(guān)SpringBoot JWT登錄權(quán)限控制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Spring Cloud中的API網(wǎng)關(guān)服務(wù)Zuul
這篇文章主要介紹了淺談Spring Cloud中的API網(wǎng)關(guān)服務(wù)Zuul,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
使用maven開(kāi)發(fā)springboot項(xiàng)目時(shí)pom.xml常用配置(推薦)
這篇文章主要介紹了使用maven開(kāi)發(fā)springboot項(xiàng)目時(shí)的pom.xml常用配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
SpringBoot實(shí)現(xiàn)配置文件的替換
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)配置文件的替換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

