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

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();
主站蜘蛛池模板: 绥宁县| 汝州市| 常德市| 温宿县| 宜宾县| 清新县| 象州县| 江陵县| 淄博市| 读书| 安康市| 陆丰市| 枣庄市| 天柱县| 灌南县| 湟源县| 图们市| 东山县| 惠来县| 莆田市| 广饶县| 大洼县| 兰考县| 郸城县| 渭南市| 宁武县| 巴马| 东阳市| 新和县| 永仁县| 巴南区| 尚义县| 吉木萨尔县| 黄平县| 当阳市| 漠河县| 大姚县| 巩义市| 舟山市| 乌拉特中旗| 永登县|