溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么理解Spring Boot認證和鑒權

發布時間:2021-11-09 13:34:22 來源:億速云 閱讀:220 作者:iii 欄目:開發技術

本篇內容介紹了“怎么理解Spring Boot認證和鑒權”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

在web應用中有大量場景需要對用戶進行安全校,一般人的做法就是硬編碼的方式直接埋到到業務代碼中,但可曾想過這樣做法會導致代碼不夠簡潔(大量重復代碼)、有個性化時難維護(每個業務邏輯訪問控制策略都不相同甚至差異很大)、容易發生安全泄露(有些業務可能不需要當前登錄信息,但被訪問的數據可能是敏感數據由于遺忘而沒有受到保護)。

為了更安全、更方便的進行訪問安全控制,我們可以想到的就是使用springmvc的攔截器(HandlerInterceptor),但其實更推薦使用更為成熟的spring security來完成認證和鑒權。

攔截器

攔截器HandlerInterceptor確實可以幫我們完成登錄攔截、或是權限校驗、或是防重復提交等需求。其實基于它也可以實現基于url或方法級的安全控制。

如果你對spring mvc的請求處理流程相對的了解,它的原理容易理解,具體可以參閱我之前的分享。

public interface HandlerInterceptor {
	/**
	 * Intercept the execution of a handler. Called after HandlerMapping determined
	 * an appropriate handler object, but before HandlerAdapter invokes the handler.
	 * 
	 * 在業務處理器處理請求之前被調用。預處理,可以進行編碼、安全控制、權限校驗等處理
	 * 
	 * handler:controller內的方法,可以通過HandlerMethod method= ((HandlerMethod)handler);獲取到@RequestMapping
	 */	
	boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
 
	/**
	 * Intercept the execution of a handler. Called after HandlerAdapter actually
	 * invoked the handler, but before the DispatcherServlet renders the view.
	 * 
	 * 在業務處理器處理請求執行完成后,生成視圖之前執行。后處理(調用了Service并返回ModelAndView,但未進行頁面渲染),有機會修改ModelAndView
	 */	
	void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception;
 
	/**
	 * Callback after completion of request processing, that is, after rendering
	 * the view. Will be called on any outcome of handler execution, thus allows
	 * for proper resource cleanup.
	 * 
	 * 在DispatcherServlet完全處理完請求后被調用,可用于清理資源等。返回處理(已經渲染了頁面)
	 * 
	 */	
	void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception;
}
//你可以基于有些url進行攔截
@Configuration
public class UserSecurityInterceptor extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] securityUrls = new String[]{"/**"};
        String[] excludeUrls = new String[]{"/**/esb/**", "/**/dictionary/**"};
        registry.addInterceptor(userLoginInterceptor()).excludePathPatterns(excludeUrls).addPathPatterns(securityUrls);
        super.addInterceptors(registry);
    }
 
    /** fixed: url中包含// 報錯
     * org.springframework.security.web.firewall.RequestRejectedException: The request was rejected because the URL was not normalized.
     * @return
     */
    @Bean
    public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
        DefaultHttpFirewall firewall = new DefaultHttpFirewall();
        firewall.setAllowUrlEncodedSlash(true);
        return firewall;
    }
 
    @Bean
    public AuthInterceptor userLoginInterceptor() {
        return new AuthInterceptor();
    }
 
    public class AuthInterceptor implements HandlerInterceptor {
        public Logger logger = LoggerFactory.getLogger(AuthInterceptor.class);
        @Autowired
        private ApplicationContext applicationContext; 
        public AuthInterceptor() {
        }
 
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            LoginUserInfo user = null;
            try {
                user = (LoginUserInfo) SSOUserUtils.getCurrentLoginUser();
            } catch (Exception e) {
                logger.error("從SSO登錄信息中獲取用戶信息失??! 詳細錯誤信息:%s", e);
                throw new ServletException("從SSO登錄信息中獲取用戶信息失??!", e);
            }
 
            String[] profiles = applicationContext.getEnvironment().getActiveProfiles();
            if (!Arrays.isNullOrEmpty(profiles)) {
                if ("dev".equals(profiles[0])) {
                    return true;
                }
            }
            if (user == null || UserUtils.ANONYMOUS_ROLE_ID.equals(user.getRoleId())) {
                throw new ServletException("獲取登錄用戶信息失??!");
            }
            return true;
        }
 
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { 
        }
 
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 
        }
    }
}

認證

確認一個訪問請求發起的時候背后的用戶是誰,他的用戶信息是怎樣的。在spring security 里面認證支持很多種方式,最簡單的就是用戶名密碼,還有LDAP、OpenID、CAS等等。

而在我們的系統里面,用戶信息需要通過kxtx-sso模塊進行獲取。通過sso認證比較簡單,就是要確認用戶是否通過會員系統登錄,并把登錄信息包裝成授權對象放到SecurityContext中,通過一個filter來完成:

@Data
@EqualsAndHashCode(callSuper = false)
public class SsoAuthentication extends AbstractAuthenticationToken {
    private static final long serialVersionUID = -1799455508626725119L; 
    private LoginUserInfo user; 
    public SsoAuthentication(LoginUserInfo user) {
        super(null);
        this.user = user;
    }
 
    @Override
    public Object getCredentials() {
        return "kxsso";
    }
 
    @Override
    public Object getPrincipal() {
        return user;
    }
 
    @Override
    public String getName() {
        return user.getName();
    }
}
public class SsoAuthenticationProcessingFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        LoginUserInfo user = (LoginUserInfo) SSOUserUtils.getCurrentLoginUser();
        SsoAuthentication auth = new SsoAuthentication(user );
        SecurityContextHolder.getContext().setAuthentication(auth);
        filterChain.doFilter(request, response);
    }
}
@Component
public class SsoAuthenticationProvider implements AuthenticationProvider {
 
    @Value("${env}")
    String env;
 
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        LoginUserInfo loginUserInfo = (LoginUserInfo) authentication.getPrincipal();
        /*
         * DEV環境允許匿名用戶訪問,方便調試,其他環境必須登錄。
         */
        if (!UserUtils.ANONYMOUS_ROLE_ID.equals(loginUserInfo.getRoleId()) || "dev".equals(env)) {
            authentication.setAuthenticated(true);
        } else {
            throw new BadCredentialsException("請登錄");
        }
        return authentication;
    }
 
    @Override
    public boolean supports(Class<?> authentication) {
        return SsoAuthentication.class.equals(authentication);
    }
}
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 
    protected void configure(HttpSecurity http) throws Exception {
        // 關閉session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and();
 
        // 允許訪問所有URL,通過方法保護的形式來限制訪問。
        http.authorizeRequests().anyRequest().permitAll();
 
        // 注冊sso filter
        http.addFilterBefore(ssoAuthenticationProcessingFilter(), UsernamePasswordAuthenticationFilter.class);
    }
 
    @Bean
    SsoAuthenticationProcessingFilter ssoAuthenticationProcessingFilter() {
        return new SsoAuthenticationProcessingFilter();
    }
}

鑒權

控制一個功能是否能被當前用戶訪問,對不符合要求的用戶予以拒絕。spring security 主要有兩種控制點:

  • 基于請求路徑的:控制某一URL模式必須符合某種要求;

  • 基于方法的:控制某一方法必須符合某種要求;

而控制形式就比較多樣化了:

  • 代碼配置;

  • xml配置;

  • 注解控制;

  • el表達式;

  • 自定義訪問控制器;

目前鑒權的需求比較簡單:登錄允許訪問,未登錄禁止訪問。因此可以定義了一個切面,控制所有需要安全控制的Controller。

spring security 提供了一些注解:

@PreAuthorize

控制一個方法是否能夠被調用,業務方法(HandlerMethod )的前置處理,比如:

@PreAuthorize("#id<10")限制只能查詢Id小于10的用戶

@PreAuthorize("principal.username.equals(#username)")限制只能查詢自己的信息

@PreAuthorize("#user.name.equals('abc')")限制只能新增用戶名稱為abc的用戶

@PostAuthorize

業務方法調用完之后進行權限檢查,后置處理,比如:

@PostAuthorize("returnObject.id%2==0")

public User find(int id) {}

返回值的id是偶數則表示校驗通過,否則表示校驗失敗,將拋出AccessDeniedException

@PreFilter

對集合類型的參數進行過濾,比如:

對集合ids中id不為偶數的進行移除 @PreFilter(filterTarget="ids", value="filterObject%2==0") public void delete(List<Integer> ids, List<String> usernames) {}

@PostFilter

對集合類型的返回值進行過濾,比如:

將對返回結果中id不為偶數的list中的對象進行移除

@PostFilter("filterObject.id%2==0") public List<User> findAll() {}

@AuthenticationPrincipal解決在業務方法內對當前用戶信息的方法
@Aspect
@Component
public class InControllerAspect {
    @Autowired
    BeforeInControllerMethods beforeInMethods;
 
    @Pointcut("execution(public * com.kxtx.oms.portal.controller.in.*.*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void hp() {
    };
 
    @Before("hp()")
    public void befor() {
        beforeInMethods.before();
    }
}
@Component
public class BeforeInControllerMethods {
    //@PreAuthorize("authenticated")要求所有訪問此方法的用戶必須登錄
    @PreAuthorize("authenticated")
    public void before() {
    }
}
//用戶信息獲取
@RequestMapping("/order/submit")
public ModelAndView findMessagesForUser(@AuthenticationPrincipal CustomUser customUser) {
    // .. find messages for this user and return them ...
}

是不是有點復雜,復雜的是表現形式,實際上需要真正理解它的目的(為了要解決什么問題)。

“怎么理解Spring Boot認證和鑒權”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女