how to set common header for all RequestMappings in a controller

Hi,
In my controller i have the following line in each requestMapping method :
        response.setHeader(contentSecurityolicy.getHttpHeader(), contentSecurityPolicy.toString());

Open in new window


There are 4 such methods... Is there any way i can avoid this so as to specify this header only once in controller and
it gets added to all the responses ?


Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rohit BajajAuthor Commented:
Also in some codebase i saw that they created a filter for adding content security policy header.
Whats the correct approach as per designing ?
Should i just add it like above in each request mapping.. or create a filter and so controller does not know about adding of this header ?
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Ok, it is not so obvious but any method annotated with @ModelAttribute will get executed for any of the 4 methods in your controller, so you could add the following to your controller...

@ModelAttribute
public void setContentSecurityPolicyHeader(HttpServletResponse response) {
        response.setHeader(contentSecurityolicy.getHttpHeader(), contentSecurityPolicy.toString());
}

Open in new window



As for what is the best design, it depends on a few factors... If you are setting the same headers from *multiple* controllers than it might be best to use a filter. Also, only if the resources that need the headers can be distinguished from those that shouldn't have them based on the request path (as that generally decides what filters to run). Also, it may depend on whether any data available in the controller affects what gets set in the headers.

From the limited info that you provide above, it looks like the controller doesn't have much input as to what is set for those headers, so my feeling would be that it would be better set in a filter.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.