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

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();
主站蜘蛛池模板: 新巴尔虎右旗| 江西省| 云和县| 岑溪市| 汝城县| 磐石市| 江津市| 洪雅县| 武川县| 古浪县| 宁乡县| 门头沟区| 敦煌市| 常山县| 陆河县| 共和县| 长丰县| 盱眙县| 西乌| 神池县| 利川市| 富民县| 友谊县| 英德市| 浑源县| 孙吴县| 凌海市| 牡丹江市| 郁南县| 康定县| 深圳市| 周宁县| 抚顺县| 孟连| 饶阳县| 崇礼县| 乐都县| 澄城县| 洛宁县| 潼关县| 玉山县|