官术网_书友最值得收藏!

Web URL

Spring Security can be used to set up URL-based authorization. HTTP Security configured can be used with Spring Security configuration to achieve the desired authorization. In many examples that we have gone through so far, we have seen pattern matching authorization. Here is one such example:

  • AntPathRequestMatcher: Uses an Ant-style pattern for URL matching:
http
.antMatcher("/rest/**")
.httpBasic()
.disable()
.authorizeRequests()
.antMatchers("/rest/movie/**", "/rest/ticket/**", "/index")
.hasRole("ROLE_USER");

In the preceding code snippet, the /rest URL's basic authentication is disabled, and for other URLs (/rest/movie, /rest/ticket and /index), users with the USER role have access. The snippet also shows single match (using antMatcher) and multiple matches (using antMatchers).

  • MvcRequestMatcher: This uses Spring MVC to match the path and then extracts variables. The matching is relative to the servlet path.
  • RegexRequestMatcher: This uses a regular expression to match the URL. It can also be used to match the HTTP method, if needed. The matching is case-sensitive and takes the form (servletPath + pathInfo + queryString):
http
.authorizeRequests()
.regexMatchers("^((?!(/rest|/advSearch)).)*$").hasRole("ADMIN")
.regexMatchers("^((?!(/rest|/basicSearch)).)*$").access("hasRole(USER)")
.anyRequest()
.authenticated()
.and()
.httpBasic();
主站蜘蛛池模板: 治县。| 石河子市| 牡丹江市| 琼海市| 蒲江县| 阳城县| 东山县| 莎车县| 嘉鱼县| 军事| 屯留县| 轮台县| 海城市| 安福县| 吉木萨尔县| 泾源县| 洛阳市| 富民县| 静安区| 依兰县| 改则县| 巫山县| 双城市| 华安县| 和硕县| 双辽市| 永定县| 富蕴县| 宁阳县| 郸城县| 多伦县| 济宁市| 聂荣县| 麻江县| 新竹市| 望谟县| 札达县| 遂昌县| 东山县| 修武县| 伊金霍洛旗|