Spring Boot JPA Database Credentials - spring-boot

Is it possible (and if yes how) to let the user enter the database connection credentials either using a login form or a console prompt after the spring boot application was launched?

You can also add them on your SpringApplication before running it. Create a map with your properties
Map<String, Object> properties = new HashMap<>();
properties.put("spring.application.name", "Test");
// properties.put("spring.jpa.hibernate.ddl-auto", "none");
properties.put("spring.datasource.url", url);
properties.put("spring.datasource.username", username);
properties.put("spring.datasource.password", password);
// properties.put("spring.liquibase.change-log", "classpath:/db/liquibase-changelog.xml");
and assign it to the application.
application.setDefaultProperties(properties);
application.run(args);

Related

Spring Boot Mail send email using acces token

I have simple mail sending functionality in project which configured in one bean.
#Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "false");
properties.setProperty("mail.smtp.socketFactory.port", "465");
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.starttls.required", "true");
javaMailSender.setHost("smtp.gmail.com");
javaMailSender.setProtocol("smtp");
javaMailSender.setUsername("username");
javaMailSender.setPassword("password");
javaMailSender.setJavaMailProperties(properties);
return javaMailSender;
}
and it works great.
Now I want to add functionality for sending emails via accessToken/refreshToken of specific email.
How to do it? What should I extend in my bean or add another bean for sending with token? I couldn't find some example which is full explained. As I understand I should add setFrom() and in setPassword() put accessToken
The use of OAUTH2 with JavaMail is explained on the JavaMail project page.
Also, you should fix these common mistakes in your code.

Alfresco Document management system in spring application

I am new to Alfresco document management system in spring, but I have done Alfresco activity workflowbefore. I want to develop Alfresco DMS in Spring.
Any body did this please send me sample model application or related web site url.
Thank you.
In case you want to connect to your Alfresco Repository
private static Session getSession(String serverUrl, String username, String password) {SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
Map<String, String> params = new HashMap<>();
params.put(SessionParameter.USER, username);
params.put(SessionParameter.PASSWORD, password);
params.put(SessionParameter.ATOMPUB_URL, serverUrl);
params.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
List<Repository> repos = sessionFactory.getRepositories(params);
if (repos.isEmpty()) {
throw new RuntimeException("Server has no repositories!");
}
return repos.get(0).createSession(); }
you only have to add your ServerUrl , Username and a Password (in default it will be admin , admin )

Spring Security OAuth2 accessToken

In my Spring Boot application I need to programmatically create a new user and obtain OAuth2 access/refresh tokens for him from internal(part of this application) OAuth2 Authorization Server.
Then, I plan to send these access/refresh tokens to some external (client) application that will interact with my first application on behalf of this user.
Is it possible to programmatically obtain OAuth2 access/refresh tokens for this user without providing password(during the programmatic creation of this user I don't want to deal with password, only username).
Yes you can, take a look at the code below
#Autowired
private TokenEndpoint tokenEndpoint;
public ResponseEntity<?> createToken(User user) {
Principal principal = new UsernamePasswordAuthenticationToken(user.getUserName(), user.getPassword(), user.getAuthorities());
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("client_id", "XXX");
parameters.put("client_secret", "XXX");
parameters.put("grant_type", "password");
parameters.put("password", user.getPassword());
parameters.put("scope", "XXX");
parameters.put("username", user.getUserName());
return tokenEndpoint.getAccessToken(principal, parameters);
}
but you are violating the OAuth2 spec. Authorization should be performed by Resource Owner.

Proxy settings in Spring Boot

My application needs to fetch an XML file from the web, as follows:
#Bean
public HTTPMetadataProvider metadataProvider()
throws MetadataProviderException {
String metadataURL = "http://idp.ssocircle.com/idp-meta.xml";
final Timer backgroundTaskTimer = new Timer(true);
HTTPMetadataProvider provider =
new HTTPMetadataProvider(backgroundTaskTimer, httpClient(), metadataURL);
provider.setParserPool(parserPool());
return provider;
}
I'm working by using a filtered network, thus the app is unable to retrieve that file.
There is a way to setup an HTTP Proxy (e.g. myproxy.eu:8080) in Spring Boot?
Alternatively, I could retrieve the XML file by using the HTTPS protocol, but I should properly setup the metadata provider in order to support an encrypted connection... How?
This is not something you can configure in spring boot, HttpClient is not using java variables.
Therefor you need to set the proxy on the httpClient manually:
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setProxyHost(new ProxyHost("your.proxy.host", 8080));
httpClient.setHostConfiguration(hostConfig);

Spring webflow unit test with spring security on

I'm trying to test a webflow controller with spring security on:
<action-state id="search">
<secured attributes="ROLE_ADMIN"/>
...
</action-state>
I'm using AbstractXmlFlowExecutionTests subclass.
Now, the test runs ok without the "secured" tag (I don't make any mocks for the security), but once I add the security tag, the test keeps finishing with success, although I anticipate a security exception to be thrown.
Any ideas why it doesn't work and how should I configure it?
Thanks in advance!
Igor
Ok, I've found the solution: I needed to add a securityListener manually.
Before startFlow:
setFlowExecutionListener(getSecurityListener(new String[] {"ROLE_ADMIN_FAKE"}));
Where
private FlowExecutionListener getSecurityListener(String[] roles) {
List<GrantedAuthority> result = new ArrayList<>();
for (String role: roles) {
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role);
result.add(authority);
}
Authentication auth = new PreAuthenticatedAuthenticationToken("Igor", "", result);
SecurityContextHolder.getContext().setAuthentication(auth);
return new SecurityFlowExecutionListener();
}

Resources