I created an api domain with Spring Framework. Part 1
It seems that Spring does not have a function to distribute access by subdomain. So, you can solve it by ** adding a special header when accessing a specific subdomain **.
For that, first let's make the filter you made work when subdomain of "api". First, add a Filter to web.xml.
web.xml
<!--Filters for subdomains-->
<filter>
<filter-name>subDomainFilter</filter-name>
<filter-class>com.TsugaruInfo.config.SubDomainFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>subDomainFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
Now let's implement a subdomain filter. Filters can be created by inheriting ** GenericFilterBean **. GenericFilterBean must override ** doFilter **, "Finally, execute FilterChain's doFilter and return the request and response to the controller." It has the property of.
In other words, you can attach the header to this request and return it to the controller. Then it looks like this.
SubDomainFilter.java
public class SubDomainFilter extends GenericFilterBean {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
//TODO auto-generated method stub
//Materialize subdomain wrapper
SubDomainHttpServletRequestWrapper httpRequest =
new SubDomainHttpServletRequestWrapper((HttpServletRequest) request);
//Get a subdomain
String subDomain = getSubDomain(httpRequest);
//If you succeed in getting the api subdomain, run the filter and exit
if(subDomain != null) {
httpRequest.addHeader("X-SUBDOMAIN", subDomain);
}
else {
//Other headers when not subdomain access
httpRequest.addHeader("X-SUBDOMAIN", "topDomain");
}
filterChain.doFilter(httpRequest, response);
}
/**
*Header judgment part Judges whether the access is as an api
* @param httpRequest
* @return
*/
private String getSubDomain(HttpServletRequest httpRequest) {
List<String> subdomains = new ArrayList<String>();
//The subdomain is "api".Returns an api string when starting with localhost
if(httpRequest.getHeader("host").matches("api.*")) {
return "api";
}
return null;
}
}
"When there is a character" api "in the" host "header of the request, I am trying to edit my own header, X-SUBDOMEIN.
So, there are some classes that are missing. HttpRequest doesn't have the ability to edit headers. So I am making a wrapper class for HttpRequest.
SubDomainHttpServletRequestWrapper.java
/**
*A subclass with getHeader and addHeader added to subdomain operations
* @author nozawa
*
*/
public class SubDomainHttpServletRequestWrapper extends HttpServletRequestWrapper {
//Header information is copied to this map
private Map<String, String> headers = new HashMap<String, String>();
public SubDomainHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
//Get request header
Enumeration<String> headers_head = request.getHeaderNames();
//Get header name
while(headers_head.hasMoreElements()){
String header = headers_head.nextElement();
Enumeration<String> values = request.getHeaders(header);
//Set header value
while(values.hasMoreElements()) {
String value = values.nextElement();
headers.put(header, value);
}
}
}
/**
*Header added
* @param name
* @param value
*/
public void addHeader(String name, String value){
headers.put(name, value);
}
/**
*Get Header
*/
public String getHeader(String name) {
if(headers.containsKey(name)) {
return headers.get(name);
}
return ((HttpServletRequest)getRequest()).getHeader(name);
}
}
This wrapper class copies the header information stored as a fixed value of the enumeration to the map of its own class. The controller considers the fields of this class as header information, so it would be nice to be able to edit this map. (Maybe it's a little clunky, please tell me if there is an efficient way)
Now let's create a controller for the api and access it!
First, Create two controller methods with the same URL.
APIPictureController.java
@RequestMapping(value="/api/picture", produces="application/json;charset=UTF-8")
@RestController
public class APIPictureController {
@RequestMapping(value = "/picture_api", method = RequestMethod.GET, headers = "X-SUBDOMAIN=api")
public String apiStreaming( HttpServletRequest request) {
request.getHeaderNames();
return "{\"text\":\"api_Sucessed\"}";
}
@RequestMapping(value = "/picture_api", method = RequestMethod.GET)
public String normalStreaming( HttpServletRequest request) {
request.getHeaderNames();
return "{\"text\":\"api_notActivate\"}";
}
These two methods are sorted according to the value of the headers field that exists in the request. Only when X-SUBDOMEIN is api, the api method works.
Let's actually access it.
When accessing localhost
api. When accessing localhost
The subdomain allowed us to tally access. ヾ (.> ﹏ <.) No All you have to do is write the function you want to write
I'm ashamed to say that it didn't work so much, so I'm changing it.
Allocate subdomains with @RequestMapping in Spring MVC. http://blog.katty.in/3025
Recommended Posts