Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

java - Is it possible to create necessary / required interfaces?

i have a little kont in my brain about structuring our code. We have a REST Backend based on SpringBoot. To handle requests regarding to security checks we use HandlerInterceptors. In some specific cases we need a specific interceptor and not our default one. The default one is registered in a 3rd party lib that no one can forget it. But i want all coders to think about this specific interceptor. Actually, i just said it to them to achieve this.

Here's my question: Is there an option to create required (or necessary) interfaces which must be implemented? This would be a way to provide our security code by lib and to have the security that every coder implemented our specific interface (also if he just does nothing with it).

pseudo code:

public interface thinkForIt(){

Object SecBean specificSecBean;

public void methodToThinkOn();

}

public SecImpl implements thinkForIt(){

@Override
public void methodToThinkOn(){
return null; // i thought about it but i do not need to do anyting!

}

If the interface thinkForIt would have any annotations like @required, users could get warning or error if they did not implement it...

Looking for a solution and thanks for your comments in advance!

question from:https://stackoverflow.com/questions/65835744/is-it-possible-to-create-necessary-required-interfaces

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your overall design is questionable; you are reinventing security code, which is always a red flag. Use Spring Security instead.

However, there's a simple way to ensure that "some bean of type Foo" has been registered with the context:

@Component
@RequiredArgsConstructor
public class ContextConfigurationVerifier {
  final Foo required;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...