Avatar
2
TungDP Beginner
TungDP Beginner
Cách nhúng đối tượng tương tự như @Autowired của Spring boot?
<em>Đặt vấn đề:</em> Lúc đọc cuốn "Những Nguyên Tắc Sống Còn Trong Lập Trình" của anh Tạ Văn Dũng, em có khá ấn tượng về cách anh giải thích về ý tưởng Inversion-of-control. Sau đó, em cũng vọc vạch làm thử 1 cái tương tự, đại ý là em làm như này: <ul> <li>Bước 1: Dùng Reflection quét package của file Main để tìm ra toàn bộ các class được khai báo trong package, đưa vào 1 HashSet.</li> <li>Bước 2: Tạo annotation @Instance, chạy vòng lặp: những class nào có đánh dấu @Instance thì khởi tạo instance cho nó, lưu vào 1 HashMap<Class<?>, Object>.</li> <li>Bước 3: Tạo annotation @Inject, và tiếp tục lặp qua HashMap ở bước 2, nếu instance nào mà có các field được đánh dấu @Inject thì gắn object đã được tạo trong HashMap(bước 2) vào field đó.</li></ul> <em>Khó khăn:</em> Nhưng em gặp vấn đề khó khi @Inject được đánh dấu vào field mà user lại sử dụng interface chứ không phải trực tiếp class. <img src="/api/v1/media/cd1910f38bf831716e6e1968e01cbfaa484f3b5b6e9846d940157d2fd6e5cdd1.png" alt="2024-02-17_15-22.png"> <em>Câu hỏi:</em> Lúc này làm sao để có thể tìm được tất cả các class implements interface đó và chọn ra được class phù hợp để inject vào ạ?
Answer
Avatar
0
Thân Nam Teacher
Thân Nam Teacher
Lỗi truyền ParameterName trong Stored Procedure trong bản Spring 3.x
Em chào mọi người.<p> Trong quá trình nâng cấp dự án sử dụng Spring 2.7.x lên bản Spring 3.x thì em gặp phải lỗi call Stored Procedure theo tên Param. </p> <p> ví dụ Khi gọi Store có tên example(@p_channel_code , @agent_code) trong SQL Server </p> <p> vị trí các param lần lượt của store khi tạo là: @p_channel_code , @agent_code </p> <p> Code (đính kèm) thì đăng kí lần lượt các param @agent_code,@p_channel_code </p> <ul> <li>> Sau khi chạy code và call Store giá trị nhận được dưới store đang nhận @agent_code = @p_channel_code, @p_channel_code = @agent_code, Em có chỉnh code đăng kí sét giá trị theo index param thì được.</li></ul> <p> ... mọi người có gặp tình trạng này không ạ. </p> <p> StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("example"); storedProcedure.registerStoredProcedureParameter("@agent_code", String.class, ParameterMode.IN); storedProcedure.registerStoredProcedureParameter("@p_channel_code", String.class, ParameterMode.IN); storedProcedure.setParameter("@agent_code", agentCode); storedProcedure.setParameter("@p_channel_code", channelCode); </p>
Answer
Avatar
1
Nguyen An Beginner
Nguyen An Beginner
Mã hóa password và username của database được cấu hình trong file application.properties của dự án Spring Boot
Xin chào tất cả mọi người và anh Dũng. Em viết API sử dụng Java Spring Boot. Khi config database trong file application.properties thì em có để cả mật khẩu và username như trong ảnh. Bây giờ em muốn mã hóa usename và password này. Vì trên dự án thực tế thì để hiển thị như thế này không tốt (liên quan đến việc bị tấn công vào database). Mọi người ai đã từng làm qua hay biết kỹ thuật làm có thể cho em hướng giải quyết được không ạ! Em xin cảm ơn. <img src="/api/v1/media/e37082725e660ffbb995cb46b75a7ca5453dee6f58837b6cf17cda86a8da3de2.png" alt="spring-db-properties.png">
Answer
Avatar
0
Thân Nam Teacher
Thân Nam Teacher
Lấy dữ liệu từ 1 trang web trong spring boot
Em chào mọi người.<p> Em có câu hỏi là làm cách nào để lấy được data từ Api của 1 trang web ạ em sử dụng spring boot. </p>
Answer
Avatar
0
Chan Beginner
Chan Beginner
maxhistory logback not working
Em chào anh và mọi người<p> Em có làm file logback và em có thêm phần "&lt; maxHistory > 30 &lt; / maxHistory >" để lưu log trong 30 ngày nhưng nó lại không hoạt đông. Em có tham khảo nhiều tài liệu trên mạng nhưng không được. Anh và mọi người ở đây đã ai làm về phần này có thể tư vấn cho em được không ạ </p> <p> Em xin cảm ơn </p>
Answer
Avatar
0
Tran Duy Tung Beginner
Tran Duy Tung Beginner
Lưu dữ liệu từ Activemq vào Elasticsearch
Em chào anh.<p> Em đang có 1 task lấy data từ Activemq đã đưọc đọc và lưu vào Elasticsearch. Tuy nhiên em chưa có hướng để làm phần này. Anh có thể giúp em được không ạ. </p> <p> Em cảm ơn ạ </p>
Answer
Avatar
0
Thân Nam Teacher
Thân Nam Teacher
Xác thực oauth2 trong spring 3.1.x và spring security 6.1.x
Em chào anh trong quá trình chuyển spring 2.4.x lên 3.1.x phần xác thực oauth2 trong dự án em gặp vấn đề <ul> <li>Spring 3.1.x không còn hỗ trợ WebSecurityConfigurerAdapter Lib spring-security-oauth2-autoconfigure</li></ul> <p> em chuyển sang các thư viện thay thế </p> <div class="markdown-block position-relative overflow-auto source-groovy"> <pre> spring-boot-starter-oauth2-resource-server 3.1.4 spring-boot-starter-oauth2-client 3.1.4 spring-security-oauth2-authorization-server 1.1.3 </pre> </div><p> và cấu hình </p> <div class="markdown-block position-relative overflow-auto source-groovy"> <pre> spring.security.oauth2.client.registration.sales.authorization-grant-type=password,authorization_code spring.security.oauth2.client.registration.sales.client-id=sales spring.security.oauth2.client.registration.sales.client-secret=password spring.security.oauth2.client.registration.sales.scope[0]=user_info spring.security.oauth2.client.provider.sales.authorization-uri=http://port:8668/sso-server/oauth/check_token </pre> </div><p> nhưng có vẻ không hoạt động </p> <p> làm sao để cấu hình lại phần oauth2 ạ </p> <p> code config ban đầu </p> <div class="markdown-block position-relative overflow-auto source-java"> <pre> <span class="pl-s">@Configuration</span> <span class="pl-s">@EnableResourceServer</span> <span class="pl-s">@EnableGlobalMethodSecurity</span>(prePostEnabled = true) <span class="pl-k">public</span> <span class="pl-k">class</span> ResourceServer <span class="pl-k">extends</span> ResourceServerConfigurerAdapter { <span class="pl-s">@Value</span>(<span class="pl-s">"${oauth2.token-endpoint-url}"</span>) private String tokenEndpointUrl; <span class="pl-s">@Value</span>(<span class="pl-s">"${oauth2.client-id}"</span>) private String clientId; <span class="pl-s">@Value</span>(<span class="pl-s">"${oauth2.client-secret}"</span>) private String clientSecret; <span class="pl-s">@Bean</span> <span class="pl-k">public</span> RemoteTokenServices tokenServices() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { RemoteTokenServices tokenServices = <span class="pl-k">new</span> RemoteTokenServices(); tokenServices.setCheckTokenEndpointUrl(tokenEndpointUrl); tokenServices.setClientId(clientId); tokenServices.setClientSecret(clientSecret); <span class="pl-k">return</span> tokenServices; } <span class="pl-s">@Override</span> <span class="pl-k">public</span> <span class="pl-k">void</span> configure(HttpSecurity http) throws Exception { http.cors().and() .requestMatchers().antMatchers(<span class="pl-s">"/api/**"</span>).and().authorizeRequests().anyRequest().authenticated(); } } </pre> </div>
Answer
Avatar
0
Thân Nam Teacher
Thân Nam Teacher
Lấy userName từ SecurityContextHolder
em chào anh, trong quá trình nâng từ spring 2.7.x lên 3.1.x phần Authentication em không lấy được username login, username login là namtv , username log ra lại mặc định là anonymousUser và login thành công<p> em k rõ là do cơ chế lấy userName đã thay đổi hay do không xác thực được ạ </p> <p> Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); </p> <p> String userName = authentication.getName(); </p>
Answer
Avatar
0
Thân Nam Teacher
Thân Nam Teacher
Thay thế Esapi trong spring boot 3.1.x
Trong dự án em đang sử dụng org.owasp.esapi <p> ( <a href="https://mvnrepository.com/artifact/org.owasp.esapi/esapi/2.5.2.0 " target="_blank">https://mvnrepository.com/artifact/org.owasp.esapi/esapi/2.5.2.0 </a> ) </p> <p> trong thư viện em này em sử dụng để mã hoá log, redirect url </p> <p> trong quá trình nâng cấp lên spring boot 3.1.1 thì thư viện trên chỉ hỗ trợ javax.servlet mà spring 3.1.x hỗ trợ jakatar.servlet vậy có thể thay thế thư viện trên bằng thư viện nào khách không ạ </p>
Answer
Avatar
0
devman Beginner
devman Beginner
làm sao để read/write dữ liệu từ Redis cluster mà bảo đảm consistency
chào mọi người,<p> Mình sử dụng Redis cluster trong dự án và Java spring boot với redis library là Lettuce. Theo mình biết thì mặc định khi write dữ liệu sẽ write vào node master, còn read sẽ read ở node slave. Mình gặp phải vấn đề khi chạy application theo kiểu active - active. khi instance 1 write, sau đó không lâu thì instance 2 sẽ lấy lại dữ liệu và xử lý nhưng redis chưa đồng bộ dữ liệu từ node master tới slave nên dẫn tới dữ liệu không đồng nhất. Nếu mình write/read trên master thì ảnh hưởng đến việc write và làm chậm chương trình. Mình đang tạm thời xử lý theo cách là dữ liệu sẽ chỉ xử lý trên một instance(hash theo key để biết được dữ liệu nào sẽ được xử lý bởi instance nào) và sử dụng thêm memory cache(caffeine cache) để cache lại dữ liệu. Liệu có cách nào để tốt hơn để xử lý vấn đề trên không ạ? và có best practice nào để cải thiện chương trình không? </p>
Answer
Avatar
0
Khoi Nguyen Beginner
Khoi Nguyen Beginner
Đăng nhập 2 hình thức jwt api(google authentication và basic login)
<ul> <li>Em làm đăng nhập bằng hai phương thức khi mà đăng nhập bằng basic thì không sao cho đến thi thêm oauth2 vào thì bị lỗi. Em có source code anh chị xem cho em với ạ <a href="https://gitlab.com/sep490_g55/backend/-/blob/khoint/src/main/java/com/capstone/backend/security/jwt/JwtAuthenticationFilter.java?ref_type=heads">link</a> , nó báo lỗi ở dòng số 47 (Cannot invoke "org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken.getPrincipal()" because "oAuth2AuthenticationToken" is null)</li> <li>Với còn 1 bug nữa là khi em upload file multipart sử dụng method post thì nó báo lỗi 401 ạ <a href="https://gitlab.com/sep490_g55/backend/-/blob/khoint/src/main/java/com/capstone/backend/controller/ResourceController.java?ref_type=heads">link</a></li> <li>Em mò mất mấy ngày mà nó vẫn không ra. Em cảm ơn anh chị</li></ul>
Answer
Avatar
0
Nguyen Thai Son Beginner
Khi nào thật sự cần dùng Reflection ạ
Khi nào thật sự cần dùng Reflection vậy anh, một cái nữa em thắc mắc với cả @Inteface ạ
Answer