what pattern should give inside http.authorizeRequests().antMatchers(GET,"/api/jobs/find/**").hasAnyAuthority("APPLICANT"); in Spring boot - spring

My controller file is
***#GetMapping(path = "/jobs/find/{status}")
** public ResponseEntity<List<Job>> getJActiveJobs(
#PathVariable("status") String status)
{
return new ResponseEntity(jobService.getActiveJobs(status), HttpStatus.OK);
}
`and in Securityconfig
http.authorizeRequests().antMatchers(GET,"/api/jobs/find/**").hasAnyAuthority("APPLICANT");
I have given this code.
on running 403 forbidden error is coming.

For example, your controller:
#RestController
#RequestMapping("/api") // (1)
class YourController {
#GetMapping(path = "/jobs/find/{status}") (2)
public void yourMethod() {
...
}
}
The pattern includes (1) and (2), in this case, it would be your configuration antMatchers("/api/jobs/find/**").

Related

Pass service's function in controller for not duplicating try catch block

I am developing REST API in Spring boot with Hibernate.
I have this function in my controller
#PostMapping("/profile")
public ResponseEntity<String> saveProfile(#Valid #RequestBody SaveProfileVM saveProfileVM,
BindingResult bindingResult)
throws JsonProcessingException {
if (bindingResult.hasErrors()) return super.fieldExceptionResponse(bindingResult);
Profile profile;
boolean optimisticLockException = true;
int retryCount = 0;
do {
try {
profile = accountService.saveProfile(saveProfileVM.getAccountId(),
saveProfileVM.getName(),
saveProfileVM.getEmail());
optimisticLockException = false;
retryCount++;
} catch (ObjectOptimisticLockingFailureException exception) {
retryCount++;
System.out.println(exception.getMessage());
}
} while (optimisticLockException && retryCount < MAX_OPTIMISTIC_LOCK_EXCEPTION_RETRY_COUNT);
return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(profile));
}
and MAX_OPTIMISTIC_LOCK_EXCEPTION_RETRY_COUNT is 3
I don't want to duplicate the do..while and try..catch blocks in every method where I need to check ObjectOptimisticLockingFailureException
do {
try{}
catch{}
} while()
Is there any way that I can pass accountService.saveProfile() to a general method that has the do..while and try..catch block so that I don't have to copy and paste the blocks to every method I need?
Every controller extends a BaseController so, it might be good to have the general method in BaseController?
#RestController
#RequestMapping("/account")
public class AccountController extends BaseController {
Can you guys give an idea please?
You can use spring-retry. More details
#Retryable(value = ObjectOptimisticLockingFailureException.class, maxAttempts = 3)
public void saveProfile(Long accountId, String name, String email){..}

Spring Boot. Different errors hanldilng for #Ccontroller and #RestController in one app

I have normal #Controller and #RestController in one App.
How it possible to handle erros in Json for REST, and redirect to error page for normal #Controller?
You can make use of the Spring Annotation #ExceptionHandler in your controller and throw Exceptions in your controller logic. For an short example I will use Java's RuntimeException. You could define your own exception and throw them
// your controller classes
#Controller
public class MyController {
#ExceptionHanlder(RuntimeException.class)
public String errorInController(){
// for your custom page
return "yourDefineErrorTemplatePage";
// if you want to redirect to the default spring page
// return "redirect:/error";
}
#RequestMapping("yourFirstEndpoint")
public String getPage(){
if(yourLogicHere){
throw new RuntimeException("Display error page");
}
return "myPage";
}
}
Your #RestController could look like the following:
#RestController
public class RestControllerClass{
#ExceptionHandler(RuntimeException.class)
public ResponseEntity<Error> errorOccured(){
// you can return just a String or define your own 'error object'
Error error = new Error("Some error occured");
return new ResponseEntity<Error>(error, Http.Status.NOT_FOUND);
}
#RequestMapping("yourSecondEndpoint")
public ResponseEntity<YourEntity> getPage(){
// the entity you want do return as json
YourEntity yourEntity = new YourEntity();
if(yourLogicHere){
throw new RuntimeException("Display error page");
}
return new ResponseEntity<YourEntity>(yourEntity, HttpStatus.OK);
}
}
Example for the Error Object:
public class Error{
private String errorMessage;
public Error(String errorMessage){
this.errorMessage = errorMessage;
}
}
I hope this small example can solve your Problem.
For more details visit: https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

Feign client and Spring retry

I have a restful service calling an external service using Spring Cloud Feign client
#FeignClient(name = "external-service", configuration = FeignClientConfig.class)
public interface ServiceClient {
#RequestMapping(value = "/test/payments", method = RequestMethod.POST)
public void addPayment(#Valid #RequestBody AddPaymentRequest addPaymentRequest);
#RequestMapping(value = "/test/payments/{paymentId}", method = RequestMethod.PUT)
public ChangePaymentStatusResponse updatePaymentStatus(#PathVariable("paymentId") String paymentId,
#Valid #RequestBody PaymentStatusUpdateRequest paymentStatusUpdateRequest);
}
I noticed the following failure 3-4 times in the last 3 months in my log file:
json.ERROR_RESPONSE_BODY:Connection refused executing POST
http://external-service/external/payments json.message:Send Payment
Add Payment Failure For other reason: {ERROR_RESPONSE_BODY=Connection
refused executing POST http://external-service/external/payments,
EVENT=ADD_PAYMENT_FAILURE, TRANSACTION_ID=XXXXXXX} {}
json.EVENT:ADD_PAYMENT_FAILURE
json.stack_trace:feign.RetryableException: Connection refused
executing POST http://external-service/external/payments at
feign.FeignException.errorExecuting(FeignException.java:67) at
feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:104)
at
feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76)
at
feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)
Is it possible to add Spring Retry on a Feign client.
What I wanted to annotate the addPayment operation with
#Retryable(value = {feign.RetryableException.class }, maxAttempts = 3, backoff = #Backoff(delay = 2000, multiplier=2))
But this is not possible, what other options do I have?
You can add a Retryer in the FeignClientConfig
#Configuration
public class FeignClientConfig {
#Bean
public Retryer retryer() {
return new Custom();
}
}
class Custom implements Retryer {
private final int maxAttempts;
private final long backoff;
int attempt;
public Custom() {
this(2000, 3);
}
public Custom(long backoff, int maxAttempts) {
this.backoff = backoff;
this.maxAttempts = maxAttempts;
this.attempt = 1;
}
public void continueOrPropagate(RetryableException e) {
if (attempt++ >= maxAttempts) {
throw e;
}
try {
Thread.sleep(backoff);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
}
#Override
public Retryer clone() {
return new Custom(backoff, maxAttempts);
}
}
Updated with sample Retryer example config based on the Retryer.Default.
If you are using ribbon you can set properties, you can use below properties for retry:
myapp.ribbon.MaxAutoRetries=5
myapp.ribbon.MaxAutoRetriesNextServer=5
myapp.ribbon.OkToRetryOnAllOperations=true
Note: "myapp" is your service id.
Checkout this Github implementation for working example
Just new a contructor Default
#Configuration
public class FeignClientConfig {
#Bean
public Retryer retryer() {
return new Retryer.Default(100, 2000, 3);
}
}
Adding this if it can help someone. I was getting connection reset using feign, as some unknown process was running on that port.
Try changing the port. Refer this to find the process running on a port
I prepared a blog post about using Spring Retry with Feign Client methods. You may consider checking the Post. All steps have been explained in the post.
This is my config. Test OK in spring boot 2.2.0.RELEASE
spring cloud Hoxton.M3.
feign.hystrix.enabled=true
MY-SPRING-API.ribbon.MaxAutoRetries=2
MY-SPRING-API.ribbon.MaxAutoRetriesNextServer=2
MY-SPRING-API.ribbon.OkToRetryOnAllOperations=true
MY-SPRING-API.ribbon.retryableStatusCodes=404,500
feign.client.config.PythonPatentClient.connectTimeout=500
feign.client.config.PythonPatentClient.readTimeout=500
hystrix.command.PythonPatentClient#timeTest(String).execution.isolation.thread.timeoutInMilliseconds=5000
java code is :
#FeignClient(name = "MY-SPRING-API",configuration = {PythonPatentConfig.class},fallbackFactory = FallBack.class)
public interface PythonPatentClient
#RequestLine("GET /test?q={q}")
void timeTest(#Param("appNo") String q);
Controller is :
#RequestMapping(value = "/test",method = {RequestMethod.POST,RequestMethod.GET})
public Object test() throws InterruptedException {
log.info("========important print enter test========");
TimeUnit.SECONDS.sleep(10L);
pom.xml additon add:
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
optional:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
#EnableRetry
#SpringBootApplication
public class ApiApplication
this is document :
https://docs.spring.io/spring-cloud-netflix/docs/2.2.10.RELEASE/reference/html/#retrying-failed-requests
https://github.com/spring-projects/spring-retry
https://github.com/spring-cloud/spring-cloud-netflix/
I resolved that by creating a wrapper on top of ServiceClient
#Configuration
public class ServiceClient {
#Autowired
ServiceFeignClient serviceFeignClient;
#Retryable(value = { ClientReprocessException.class }, maxAttemptsExpression = "#{${retryMaxAttempts}}", backoff = #Backoff(delayExpression = "#{${retryDelayTime}}"))
public void addPayment( AddPaymentRequest addPaymentRequest){
return serviceFeignClient.addPayment(addPaymentRequest);
}
}

Resteasy multiple resource path waring

I have a service class with 2 GET request like following and in the log is is always giving me warning about Multiple resource method match Request
#Path("/a")
class Service{
#Path("/{name}"
#GET
public A methodA(#PathParam("name") String name){return a;}
#Path("/status")
#GET
public B methodB(){return b;}
}
Can anybody have any idea why is that??
I am using rest-easy version 3.0.8 with spring 4.x.x
Considering my comment in your question above, I would rewrite my controller to this:
#Path("/a")
class Service{
#Path("/{name}"
#GET
public ResponseEntity methodA(#PathParam("name") String name){
if("status".equals(name) {
return new ResponseEntiry(b, OK);
} else {
return new ResponseEntiry(a, OK);
}
}
}

How to do URL Rewrite in Zuul Proxy?

One of the request that comes to my Zuul Filter is of URI /hello/World which i want to redirect to /myapp/test. This /myapp/test is a service that is registered in Eureka.
zuul:
routes:
xyz:
path: /hello/World
url: http://localhost:1234/myapp/test
stripPrefix: true
When i try the above configuration, the incoming URI is suffixed to the configured URL like http://localhost:1234/myapp/test/World . Few of the links which i came across seem to be stating that URL Rewrite feature is not yet available in Zuul.
Is there any other way this can be done at the Zuul Layer ?
Note: At this point of time, i cannot do this reverse proxying in the Webserver or any other layer since, my Zuul filter is the one that is receiving the request directly.
Using #Adelin solution, with little improvements
Use 'url' property as path to prepend for customizing the Url rewriting (I have disabled Eureka in my example) :
ribbon.eureka.enabled=false
zuul.routes.route1.path=/route1/**
zuul.routes.route1.serviceId=service1
zuul.routes.route1.url=/path/to/prepend
service1.ribbon.listOfServers=http://server1
Then implement the following filter :
/**
* Fixing missing URL rewriting when using ribbon
*/
#Component
public class CustomPathZuulFilter extends ZuulFilter {
#Autowired
private ZuulProperties zuulProperties;
#Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}
#Override
public int filterOrder() {
return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1;
}
#Override
public boolean shouldFilter() {
// override PreDecorationFilter only if executed previously successfully
return RequestContext.getCurrentContext().getFilterExecutionSummary().toString()
.contains("PreDecorationFilter[SUCCESS]");
}
#Override
public Object run() {
final RequestContext context = RequestContext.getCurrentContext();
if (context.get(FilterConstants.SERVICE_ID_KEY) == null || context.getRouteHost() != null) {
// not a Ribbon route
return null;
}
// get current ZuulRoute
final String proxy = (String) context.get(FilterConstants.PROXY_KEY);
final ZuulRoute zuulRoute = this.zuulProperties.getRoutes().get(proxy);
// patch URL by prefixing it with zuulRoute.url
final Object originalRequestPath = context.get(FilterConstants.REQUEST_URI_KEY);
final String modifiedRequestPath = zuulRoute.getUrl() + originalRequestPath;
context.put(FilterConstants.REQUEST_URI_KEY, modifiedRequestPath);
// patch serviceId because :
// - has been set to route.location in PreDecorationFilter
// - route.location has been set to zuulRoute.location in SimpleRouteLocator
// - zuulRoute.location return zuulRoute.url if set
context.set(FilterConstants.SERVICE_ID_KEY, zuulRoute.getServiceId());
return null;
}
}
Now calls to /route1 will be proxified to http://server1/path/to/prepend
This solution is also compatible with co-existing routes not using Ribbon.
Example of a co-existing route not using Ribbon :
zuul.routes.route2.path=/route2/**
zuul.routes.route2.url=http://server2/some/path
Calls to /route2 will be proxified to http://server2/some/path by SimpleHostRoutingFilter (if not disabled)
Here is a posted solution in the link by #Vikash
#Component
public class CustomPathZuulFilter extends ZuulFilter
{
#Override
public String filterType() {
return "pre";
}
#Override
public int filterOrder() {
return PreDecorationFilter.FILTER_ORDER + 1;
}
#Override
public boolean shouldFilter() {
return true;
}
#Override
public Object run() {
RequestContext context = RequestContext.getCurrentContext();
Object originalRequestPath = context.get(REQUEST_URI_KEY);
String modifiedRequestPath = "/api/microservicePath" + originalRequestPath;
context.put(REQUEST_URI_KEY, modifiedRequestPath);
return null;
}
}
Have you tried creating a preFilter or even a routeFilter ?
That way you can intercept the request, and change the routing.
See Zuul Filters

Resources